SMA Runbook to set VM custom property… (or run PS script)

A word…

Having worked with many clients over the years, the commonality between many of them an under-utilization of automation. Even the biggest of Microsoft shops fall short when it comes to automation, regularly being confused about the options they have and often fear of introducing a new tool to their organisation.

As a Hyper-V data center operator, I leverage automation as often as I can. This has been a concept we have tried to foster since the days of having scores of scripts triggered from scheduled tasks…

Moving forward a few years, System Center included Orchestrator, formerly Opalis, which was a GUI based orchestration engine. I personally thought it was excellent and we still use this today to facilitate many of our core business functions. Buts good as Orchestrator is, it requires a bit of setup and the integration packs are still often lacking behind the services we were connecting to. When Service Management Automation hit us and we were able to leverage this through Windows Azure Pack, this allowed us to automate using PoSH workflows. All that required was the PoSH modules which the integration packs of SCOrch were always trailing behind.

So whilst the workflow I am demonstrating below is not overly exciting, what I am trying to encourage is the use of the various automation tools from Microsoft. In this case, Service Management Automation.

 

Now on with the show…

 

Periodically I’d run a PoSH script to set a custom property called DataStore as per this previous post

Not overly exciting but gave me a birds eye view of where each VM lives in respect to the storage.

image

 

After telling myself for about 2 years that’d I’d move it to a SMA Runbook and schedule it, I finally found 10 minutes to do that this morning..

 

Firstly, some ground rules:

  1. You have working SMA infrastructure
  2. You have created to custom property in SCVMM
  3. You have the appropriate PS modules on your workers (point 1 should cover this)
  4. You test this before publishing…
  5. Use at your own risk Open-mouthed smile

 

Ok, we’re good to go..

 

Step 1:

Create your VMM server variable in your assets. Mine is called VMMServer as shown below.

image

 

The value should be the fqdn of your VMM server. If you have a HA instance of SCVMM, then use the role name. I.e. what you connect your console to

image

 

Step 2:

Create a new Runbook. I suggest giving it a brief description

Leave the tags field blank

image

 

Step 3:

Enter the following workflow code

workflow Set-SCVMMDatastore
{
    [string]$VMMServer = Get-AutomationVariable -Name 'VMMServer'
    inlinescript 
    {
        get-scvmmserver -computername $using:VMMServer | out-null
        # The engine room
        $VMS = Get-SCVirtualMachine
        ForEach ($VM in $VMS) 
        {
            IF($VM.MostRecentTaskUIState -ne "Running")
            {
                $DS = $null
                $dsprop = Get-SCCustomProperty -Name Datastore
                $DataStore = Get-SCCustomPropertyValue -CustomProperty $dsprop -InputObject $VM
                $Location = $VM.location
                # If using CSV connected to hosts.. i.e. iSCSI and FC
                IF ($Location -like "C:\ClusterStorage*")
                    {
                    $DS = $location.Split("{\}")[2]
                    }
                # For SMB3 Shares - modify as required
                IF ($Location -like "\\*")
                    {
                    $DS = $location.Split("{\}")[3]
                    }
                IF ($DS -eq $null){$DS = "Local Storage"}
                IF ($DS -like "Volume*"){$DS = "iSCSI_" + $DS}
                IF ($DS -like "Disk0*"){$DS = "SMB01P01_" + $DS}
                IF ($DS -like "SMB02P02_Disk0*"){$DS = $DS}
                IF ($DS -like "*Hyper-V"){$DS = "SMB3_" + $DS}
                
                # Update the property
                IF ($DataStore.Value -ne $DS) 
                {
                    Write-Output "DataStore for $VM is out of date... Setting to '$DS'"
                    Set-SCCustomPropertyValue -CustomProperty $dsprop -InputObject $VM -Value $DS
                    $VMSR += $VM
                }
            }
            ELSE
            {
                Write-Output "$($VM.Name) has a job running - skipping."
            }
        }
        IF($VMSR.count -gt 0)
        {
            Write-Output "Refreshing $(VMSR.count) VMs"
            ForEach ($VMR in $VMSR)
            {
                Write-Output "Refreshing VM "$VMR.Name
                Read-SCVirtualMachine -VM $VMR
            } 
        }
        ELSE
        {
            Write-Output "No VMs to update"
        }
    }
}

 

Yes I know, not the worlds most efficient workflow. But I didn’t have the time to optimize the script and this is more about demonstrating how easy it is to use SMA to schedule your fabric management tasks..

 

Step 4:

Hit the “Test” button to kick off the Runbook.

After a minute or two you should see an output similar to the below.

image

 

The SCVMM jobs view will show a Set-SCCustomPropertyValue and a Read-SCVirtualMachine job.

Note the job owner is your SMA Runbook Servers service account.

image

The Test VM custom properties before:

image

Test VM custom properties after:

image

Press “Test” button again and output confirms the workflow has nothing to do..

image

 

Step 5:

Publish the Runbook and Schedule

Hit the “Publish” button

image

and then go to the Schedule tab and add a new schedule

Alternatively use and existing schedule from your assets

image

Configure the schedule to your desired time. Mine is at 4am.

image

 

Step 6:

Come back tomorrow and review your handiwork..

 

Hope this little demo demonstrates how simple it is to leverage automation in your environments freeing up your time to focus on the fun tasks.

Enjoy!
Dan

Leave a Reply

Your email address will not be published. Required fields are marked *