alex_aldo - Fotolia

Tip

These IT automation scripts take little effort and save a lot of work

Doing some IT tasks by hand is doing them wrong. Whether to provision components, research an issue or report on performance, IT automation scripts are powerful and easy to write.

IT administrators are under pressure to do more with less: less time, fewer resources and never enough money. Automating steps for servers and application deployments can lift some repetitive tasks off their lists.

Deploying and managing IT environments without automation is the wrong way to work. Automation scripts are no longer nice to have on the job -- they are a business necessity.

The big secret is that scripting for automation isn't complicated. The below examples of useful automation scripts demonstrate how IT teams can perform common tasks in a way that saves time and money.

Commissioning and configuring hosts

IT administrators frequently build hosts: a time-intensive manual process, even when simply setting up a post-VM configuration. Manual setup leaves room for error; even in a small environment, it can result in subtle differences in configurations between hosts that were supposed to be identical.

The cure is desired state configuration (DSC), which programmatically defines how a machine should look post deployment. Chef is one popular DSC tool, and is used in this scripting automation example.

The administrator writes a Chef recipe that is held in a cookbook, which is the tool's terminology for a script in a folder with some extra layouts and configuration details.

It takes time to create an initial set of recipes and cookbooks, but the results pay back this investment. To hone and develop the deployment becomes a simple case of deploying the appropriate recipes after the initial host build. An administrator can copy, change and deploy the recipe or cookbook again and again, saving time and ensuring a consistent platform configuration.

Skeptical that setting up a DSC environment will alleviate work rather than create new work? Consider the time it takes to set up web servers for test, development and production -- a simple script configures a web server in the time it takes to pour a cup of coffee.

Chef and its underlying language Ruby, along with PowerShell, make setting up a web server on Windows systems as easy as:

powershell_script 'Install IIS' do
 code 'Add-WindowsFeature Web-Server'
 guard_interpreter :powershell_script
 not_if "(Get-WindowsFeature -Name Web-Server).Installed"
end

The code above, borrowed from Chef, illustrates the simplicity of working with DSC automation. The administrator could further add web content and configuration; it's easy to copy the recipe and install a new role.

DSC and other initial deployment tools enable IT organizations to build a scalable infrastructure. To deploy the same configuration repeatedly from development to production becomes trivial, and it reduces the risk of configuration errors creating issues when new releases occur. Similarly, an administrator could configure dozens of different configurations, depending on needs, in a controlled and documented manner.

Some IT organizations will pair the desired state configuration management tool with an orchestration tool, but even without such tools, automation scripts provide an effective way to configure and manage an environment.

The allure of automated testing

An IT operations admin isn't typically responsible for software testing. Instead, the developers and quality assurance (QA) team oversee this task. But automated testing is a prominent area under the broader IT automation umbrella, and plays an important role in continuous integration and delivery.

With test automation, software development teams use specialized tools to execute test scripts that ensure application code meets requirements and runs properly before production deployment. Just as with the other script automation use cases outlined in this article, automated tests save time, eliminate manual errors and boost efficiency and repeatability.

Some popular test automation technologies include Selenium, Appium and Cucumber.

Information extraction and reporting

Scripting for automation aids in research as well. An administrator gets frequent ad hoc queries such as "Which hosts are my virtual machines on?" or "Who has the virtual machine administrator role?"

Use the below PowerShell script to report virtual machine status:

$vmsToCheck = "get-content c:\scripts\vmdata.txt"
foreach ($vm in $vmsToCheck) {
 
    get-vm $vm | select vmname, vmhost, NumCPU, RAM | export-csv "c:\data\result.csv"
}

This useful automation script, which took less than two minutes to write, reports on a group of machines in the vmdata.txt file. It saves time whenever users or server owners ask the administrator for machine details. Instead of researching and writing up a report each time by hand, the administrator simply inputs those twenty or so machines to report on and runs the script to generate a report.

In another example, the administrator can check the status of VMware tools for a set of virtual machines:

foreach ($VM in Get-Content c:\scripts\toolslist.txt) {
 
 get-vm $VM | % { get-view $_.id } | 
 select Name, @{ Name="ToolsVersion";
Expression={$_.config.tools.toolsVersion}},@{ Name="ToolStatus";
Expression={$_.Guest.ToolsVersionStatus}}| Export-Csv -Append -Force -
NoTypeInformation C:\scripts\toolsinstalled.csv 

}

Although these are trivial automation script examples, they turn real chores into simple tasks.

Author's noteBoth of the PowerShell scripts above require that the user connect to a VMware vCenter server before running, or that the vCenter connection is included in the script.

Automated reporting

Sometimes the reporting available from within an application doesn't cut it. Automation scripts give an administrator access to more advanced data when combined with the power of RESTful APIs.

Although a bit more complex than an average script, the RESTful language lets administrators import data from web-based systems via HTTP requests. With a supported API and scripting skills, an administrator can generate reports or other outputs that satisfy exact requirements. For example, I use a script to export error reports from a system that isn't good at recording them. Using the RESTful API, I created an error log that can be scraped for data as needed.

Scripting for automation is essential in today's automated IT world, as these examples illustrate. If you haven't already started to script and automate, jump on it now.

Next Steps

Guide: When and how to use REST

Network automation scripts: Separating myth from reality

An initiation into infrastructure automation tools and methods

Manage VMs with Azure Automation State Configuration

Dig Deeper on Systems automation and orchestration