2 Get started automating

 

This chapter covers

  • Applying the concept of phased automations
  • Examples of how to create reusable functions
  • How to store your functions in a module

In the last chapter, you read about how to make your automation project a success by using the concepts of phases and building blocks and how those apply to PowerShell. In this chapter, you will see how to take a simple script and turn it into a reusable building block you can use anywhere. You will do this by creating a script to clean up old log files and turn it into a building block by thinking like an automator.

You will also learn how to store these building blocks for use across multiple automations. Whether you are writing a simple script to automate a repetitive task or working with a much more extensive script, knowing how to use a phased approach to your automation can save you a lot of time, money, and stress.

2.1 Cleaning up old files (your first building blocks)

In this section, you are going to write a simple script (automation) to clean up old log files. In doing so, you will apply the concept of building blocks to your script creation.

As always, you start with your requirements gathering. You know that you need to remove old logs to keep the drive from filling up. You also understand that the logs must be retained for at least seven years, but after 30 days, they can go into cold storage.

2.1.1 Your first function

 
 
 

2.1.2 Returning data from functions

 
 

2.1.3 Testing your functions

 
 
 
 

2.1.4 Problems to avoid when adding functions to scripts

 
 

2.1.5 Brevity versus efficiency

 
 

2.1.6 Careful what you automate

 
 
 
 

2.1.7 Putting it all together

 
 
 

2.2 The anatomy of PowerShell automation

 
 

2.2.1 When to add functions to a module

 
 

2.2.2 Creating a script module

 
 
 

2.2.3 Module creation tips

 

Summary

 
 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest