6 Making adaptable automations

 

This chapter covers

  • Using event handling to account for known errors
  • Creating dynamic functions
  • Using external data in your scripts

One of the toughest challenges you will face with automation is figuring out how to make things as efficient and maintainable as possible. The best way to achieve that is by making your code as smart and adaptable as possible. As you will see, adaptable scripting can mean many different things. For example, something as simple as adding parameters to a function makes your code more adaptable. But in this chapter, we will take it to the next level by making functions that can account for potential known errors and resolve them and make a function that can create dynamic if/else statements on the fly. And, at the end, you will see how you can tie all these functions together into a dynamic automation.

To demonstrate this, we will build an automation to perform some basic server setup tasks. This automation will perform the following steps:

  1. Install Windows Features and Roles
  2. Stop and disable unneeded services
  3. Configure the security baseline setting
  4. Configure the Windows firewall

6.1 Event handling

6.1.1 Using try/catch blocks for event handling

6.1.2 Creating custom event handles

6.2 Building data-driven functions

6.2.1 Determining your data structure

6.2.2 Storing your data

6.2.3 Updating your data structure

6.2.4 Creating classes

6.2.5 Building the function

6.3 Controlling scripts with configuration data

6.3.1 Organizing your data

6.3.2 Using your configuration data