Logical steps to automate a task

Logical steps to automate a task

In this blog I’m going to detail the logic steps that I use to automate a task, I can’t say this is the best way or the most efficient way to complete the task but it is the process that I have developed over the last decade for myself to automate tasks.

First off we start with the what, when, why, and how of the task so in this example let’s detail the clean-up of a folder on a group of computers let’s call it:

c:\users\<username>\appdata\roaming\Apple computer (you know the folder that roams with the backup of your iPhone)

So we have the What which is empty the apple computer folder

The When well you would want to complete this task before the roaming profiles starts to copy the files up to your file server.

The Why is a simple one where we want to stop the replication of a stupid amount of data when you log on and off the computer.

And the How in this circumstance we are going to use an SCCM compliance settings.

These all group together to provide us a purpose as to why we are making this change. The next step is to start stepping out the process, I personally prefer to do this with a pencil and paper with a rubber, as it allows you to remove the computer element from your planning. In saying that Visio does the task just as well if not better as you don’t have to keep rubbing things out if you keep missing steps. My process flowcharts look something like this:

remediate

So we have a simple process mapped out now which gives us a high level break down of the steps required to complete the task.

From here I like to put fragments of code that I’m going to use next to each process step, this give me the ability to capture where I use the same process repeatedly where it might suit a sub routine or function.

In this example I will use VBscript as it might need to run on machines that don’t have PowerShell I’m looking at you XP.

remediatewithscript

So from here we can create a script that looks something like this:

 


strComputer = "."
dim OS
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
OS = objOperatingSystem.Caption
Next
 
if instr(os, "XP") or instr(os, "2003") then
                userpath = "c:\documents and settings"
else
                userpath = "c:\users"
end if
 
set FSO = createobject("Scripting.FileSystemObject")
userfolder = fso.getfolder(userpath)
 
for each subfolder in userfolder
                if FSO.folderexists(subfolder.path & "\appdata\roaming\Apple computer") then
                                FSO.deletefolder(subfolder.path & "\appdata\roaming\Apple computer")
                end if
next

 

This will remediate the issue on all of the targeted computers. Now we need to make it so we can detect if the folder actually exists on the computer to use the SCCM Compliance Settings.

The process is very much the same for the Detection method but rather than deleting the folder if we find it we will return back that we have found the folder and it would look something like this:

Discovery

Obviously we can use the same process to create the script to detect the folder, and reuse a good part of our previous script, on the process diagram it would look like this:

Discoverywithscript

Which results in a script that looks like this:

 


strComputer = "."
dim OS
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
OS = objOperatingSystem.Caption
Next
 
if instr(os, "XP") or instr(os, "2003") then
                userpath = "c:\documents and settings"
else
                userpath = "c:\users"
end if
 
set FSO = createobject("Scripting.FileSystemObject")
userfolder = fso.getfolder(userpath)
 
noncompliant = 0
for each subfolder in userfolder
                if FSO.folderexists(subfolder.path & "\appdata\roaming\Apple computer") then
                                noncompliant = noncompliant + 1
                end if
next
 
if noncompliant > 0 then
                wscript.echo "Non-Compliant"
else
                wscript.echo "Compliant"
end if

From here we can create a script based configuration item and apply it to a configuration baseline in SCCM with remediation.

We now have everything required to create our new Configuration Item in SCCM 2012 to create this we need to open the SCCM 2012 Console and navigate to Assets and Compliance, Compliance Settings, Configuration Items.

From here we can select the Create Configuration Item task from the Ribbon

createconfigitem

On the Create Configuration Item Wizard form enter a descriptive name something like U – Cleanup Apple Backups which provides a quick view of what the task is, I would also strongly encourage the use of the Description box to save you needing to try to pull apart your Configuration Item 6 months down the track when you have to fix it.

On the next page we can define the Platform we are wanting to support, as we handle this in the script we can just leave it with the default of select all.

Now we can create a new Settings object and fill it out to look something like this:

createsetting

You will see the Red error mark next to the Add Script for Discovery Script, let’s go ahead and add the script we create earlier making sure to change the script language to vbscript

discoscript

Complete the same steps to add the Remediation script, and select OK to close out of the Create Setting form.

On the Compliance Rules stop in the Create Configuration Item Wizard we are now going to define on what condition we will declare noncompliance needing remediation, which looks something like this:

compliacerule

Note the field for “the following values” is where we handle the command we echo in the script.

From here we can complete the wizard and apply the new Configuration Item to a baseline and your systems and clean up the Apple backup folder before it clogs up your roaming profiles.

I hope this has provided an insight in to a process that you can use to create automated processes.

Good Luck

Steve

2 thoughts on “Logical steps to automate a task

Leave a Reply