Hello Readers/Viewers!

There comes a time when a project’s complexity increases to the point of requiring pre-requisites. The follow-up post to Automation–Service Management Automation Runbook Spotlight–Virtual Machine Startup by Priority (Part 1) is one of these instances. As described in Automation–Service Management Automation Runbook Spotlight–Virtual Machine Startup by Priority (Part 1.5), I am reintroducing the concept of data storage via VMM Custom Properties. In that post I explain the need for this post (the actual pre-requisite) and make a promise to deliver examples for the following tasks:

  • Create VMM Custom Property
  • Update VMM Custom Property Value
  • Get VMM Custom Property Value
  • Remove VMM Custom Property Value
  • Remove VMM Custom Property

This is the post where I deliver on that promise.


Example SMA Runbook Rundown!

The following PowerShell v3 Workflow examples have been implemented and tested in SMA. Each of them can be copy/pasted from here, or imported from the available PS1 files included in the TechNet Gallery Contribution (the link can be found below).

Create VMM Custom Property

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
workflow Create-VMMCustomProperty
{
    param
    (
    [PSCredential]$Creds,
    [string]$VMMServer,
    [string]$CustomPropertyScope,
    [string]$CustomPropertyName
    )

    InlineScript {

        Import-Module virtualmachinemanager
        Get-VMMServer -ComputerName $Using:VMMServer | Out-Null
   
        New-SCCustomProperty -Name $Using:CustomPropertyName -AddMember $Using:CustomPropertyScope

    } -PSComputerName $VMMServer -PSCredential $Creds

}

Note     This example can be found in the SMA-VMMCustomPropertyManagement.zip as Create-VMMCustomProperty.ps1

Update VMM Custom Property Value

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
workflow Update-VMMCustomPropertyValue
{
    param
    (
    [PSCredential]$Creds,
    [string]$VMMServer,
    [string]$CloudName,
    [string]$VMName,
    [string]$CustomPropertyScope,
    [string]$CustomPropertyName,
    [int]$CustomPropertyValue
    )

    InlineScript {

        Import-Module virtualmachinemanager
        Get-VMMServer -ComputerName $Using:VMMServer | Out-Null

        $Cloud = Get-SCCloud -Name $Using:CloudName

        if ($Using:CustomPropertyScope -eq "VM") { $InputObject = Get-SCVirtualMachine -Name $Using:VMName -Cloud $Cloud }
        else { $InputObject = $Cloud }

        Get-SCCustomProperty -Name $Using:CustomPropertyName | Set-SCCustomPropertyValue -InputObject $InputObject -Value $Using:CustomPropertyValue

    } -PSComputerName $VMMServer -PSCredential $Creds

}

Note     This example can be found in the SMA-VMMCustomPropertyManagement.zip as Update-VMMCustomPropertyValue.ps1

Get VMM Custom Property Value

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
workflow Get-VMMCustomPropertyValue
{
    param
    (
    [PSCredential]$Creds,
    [string]$VMMServer,
    [string]$CloudName,
    [string]$VMName,
    [string]$CustomPropertyScope,
    [string]$CustomPropertyName
    )

    InlineScript {

        Import-Module virtualmachinemanager
        Get-VMMServer -ComputerName $Using:VMMServer | Out-Null

        $Cloud = Get-SCCloud -Name $Using:CloudName

        if ($Using:CustomPropertyScope -eq "VM") { $InputObject = Get-SCVirtualMachine -Name $Using:VMName -Cloud $Cloud }
        else { $InputObject = $Cloud }

        $CustomProperty = Get-SCCustomProperty -Name $Using:CustomPropertyName
        Get-SCCustomPropertyValue -InputObject $InputObject -CustomProperty $CustomProperty

    } -PSComputerName $VMMServer -PSCredential $Creds

}

Note     This example can be found in the SMA-VMMCustomPropertyManagement.zip as Get-VMMCustomPropertyValue.ps1

Remove VMM Custom Property Value

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
workflow Remove-VMMCustomPropertyValue
{
    param
    (
    [PSCredential]$Creds,
    [string]$VMMServer,
    [string]$CloudName,
    [string]$VMName,
    [string]$CustomPropertyScope,
    [string]$CustomPropertyName
    )

    InlineScript {

        Import-Module virtualmachinemanager
        Get-VMMServer -ComputerName $Using:VMMServer | Out-Null

        $Cloud = Get-SCCloud -Name $Using:CloudName

        if ($Using:CustomPropertyScope -eq "VM") { $InputObject = Get-SCVirtualMachine -Name $Using:VMName -Cloud $Cloud }
        else { $InputObject = $Cloud }

        $CustomProperty = Get-SCCustomProperty -Name $Using:CustomPropertyName
        $CustomPropertyValue = Get-SCCustomPropertyValue -InputObject $InputObject -CustomProperty $CustomProperty

        if ($CustomPropertyValue) { Remove-SCCustomPropertyValue -CustomPropertyValue $CustomPropertyValue }

    } -PSComputerName $VMMServer -PSCredential $Creds

}

Note     This example can be found in the SMA-VMMCustomPropertyManagement.zip as Remove-VMMCustomPropertyValue.ps1

Remove VMM Custom Property

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
workflow Remove-VMMCustomProperty
{
    param
    (
    [PSCredential]$Creds,
    [string]$VMMServer,
    [string]$CustomPropertyScope,
    [string]$CustomPropertyName
    )

    InlineScript {

        Import-Module virtualmachinemanager
        Get-VMMServer -ComputerName $Using:VMMServer | Out-Null
   
        Get-SCCustomProperty -Name $Using:CustomPropertyName | Remove-SCCustomProperty

    } -PSComputerName $VMMServer -PSCredential $Creds

}

Note     This example can be found in the SMA-VMMCustomPropertyManagement.zip as Remove-VMMCustomProperty.ps1


Calling the Runbooks

The following PowerShell v3 Workflows contain all the necessary scripts to call the above (5) Runbook/Workflow examples. In fact, since each example will likely be executed individually, the following script snippets have been broken up by atomic function (with common script portions included at the top).

Note     Each of the specific example script calls below leverage an array of “:” delimited data which is parsed (split) and passed to the calls by specific array placement.  You can certainly generate and pass data any way that you would like, this was just my way to encapsulate all the data necessary for the examples provided.

Top (and ending bracket) of Script with Common Variables

001
002
003
004
005
006
007
008
workflow Execute-VMMCustomPropertyRunbooks
{
$Creds = Get-AutomationPSCredential -Name 'Contoso Creds'
$VMMServer = "VMM01"

    <# Insert specific example script call here #>

}

Note     Workflow name (Execute-VMMCustomPropertyRunbooks) and variable usage ($Creds and $VMMServer) are provided as an example, and can be leveraged when calling the other script snippets listed below (as indicated by the commented portion). Also, Line 3 above ($Creds) leverages an SMA Credential Variable. Please refer to this blog post for more information on this type of variable (to be found in the Overview of the SMA Runbooks section of that post).

Calling the Create-VMMCustomProperty Workflow/Runbook

001
002
003
004
005
006
007
008
009
<# Calling Create-VMMCustomProperty #>
$VMMCPCreateData = @("Cloud:Startup Priority Groups","VM:Startup Group")
   
foreach($VMM in $VMMCPCreateData) {
   
    $VMMData = $VMM.Split(":")
   
    Create-VMMCustomProperty -Creds $Creds -VMMServer $VMMServer -CustomPropertyScope $VMMData[0] -CustomPropertyName $VMMData[1]
}

Calling the Update-VMMCustomProperty Workflow/Runbook

001
002
003
004
005
006
007
008
009
<# Calling Update-VMMCustomProperty #>
$VMMCPUpdateValueData = @("Cloud:Primary Cloud:Primary Cloud:Startup Priority Groups:3","VM:Primary Cloud:spftest01:Startup Group:1","VM:Primary Cloud:spftest02:Startup Group:1","VM:Primary Cloud:spftest03:Startup Group:2","VM:Primary Cloud:spftest04:Startup Group:3")
   
foreach($VMM in $VMMCPUpdateValueData) {
   
    $VMMData = $VMM.Split(":")
   
    Update-VMMCustomPropertyValue -Creds $Creds -VMMServer $VMMServer -CloudName $VMMData[1] -VMName $VMMData[2] -CustomPropertyScope $VMMData[0] -CustomPropertyName $VMMData[3] -CustomPropertyValue $VMMData[4]
}

Calling the Get-VMMCustomPropertyValue Workflow/Runbook

001
002
003
004
005
006
007
008
009
010
011
012
<# Calling Get-VMMCustomPropertyValue #>
$VMMCPGetValueData = @("Cloud:Primary Cloud:Primary Cloud:Startup Priority Groups","VM:Primary Cloud:spftest01:Startup Group","VM:Primary Cloud:spftest02:Startup Group","VM:Primary Cloud:spftest03:Startup Group","VM:Primary Cloud:spftest04:Startup Group")
$CustomPropertyValue = @()
       
foreach($VMM in $VMMCPGetValueData) {
       
    $VMMData = $VMM.Split(":")
           
    $CustomPropertyValue += $VMMData[3] + " : " + $VMMData[2] + " : " + (Get-VMMCustomPropertyValue -Creds $Creds -VMMServer $VMMServer -CloudName $VMMData[1] -VMName $VMMData[2] -CustomPropertyScope $VMMData[0] -CustomPropertyName $VMMData[3]).Value
}
       
$CustomPropertyValue

Note     While the example directly above does work via PowerShell v3 Workflow, it does not function with the Preview Release of SMA.

As this is a “Get” example, it seems only logical to show what the output actually looks like (execution from PowerShell):

image

Please refer to the example directly below for a version of the PowerShell which works in the Preview Release of SMA as well (though, the output lacks the specified formatting above):

001
002
003
004
005
006
007
008
009
010
011
<# Calling Get-VMMCustomPropertyValue (alternate) #>
$VMMCPGetValueData = @("Cloud:Primary Cloud:Primary Cloud:Startup Priority Groups","VM:Primary Cloud:spftest01:Startup Group","VM:Primary Cloud:spftest02:Startup Group","VM:Primary Cloud:spftest03:Startup Group","VM:Primary Cloud:spftest04:Startup Group")
       
foreach($VMM in $VMMCPGetValueData) {
       
    $VMMData = $VMM.Split(":")
           
    $CustomPropertyValue += Get-VMMCustomPropertyValue -Creds $Creds -VMMServer $VMMServer -CloudName $VMMData[1] -VMName $VMMData[2] -CustomPropertyScope $VMMData[0] -CustomPropertyName $VMMData[3]
}
       
$CustomPropertyValue

Note     The main differences include: Removal of $CustomPropertyValue as a declared array; Removal of anything outside $CustomPropertyValue being assigned the object results from the execution of Get-VMMCustomPropertyValue

Here is a look at the output as seen from SMA (differences are immediately apparent):

image

Calling the Remove-VMMCustomPropertyValue Workflow/Runbook

001
002
003
004
005
006
007
008
009
<# Calling Remove-VMMCustomPropertyValue #>
$VMMCPRemoveValueData = @("Cloud:Primary Cloud:Primary Cloud:Startup Priority Groups","VM:Primary Cloud:spftest01:Startup Group","VM:Primary Cloud:spftest02:Startup Group","VM:Primary Cloud:spftest03:Startup Group","VM:Primary Cloud:spftest04:Startup Group")
   
foreach($VMM in $VMMCPRemoveValueData) {
   
    $VMMData = $VMM.Split(":")
   
    Remove-VMMCustomPropertyValue -Creds $Creds -VMMServer $VMMServer -CloudName $VMMData[1] -VMName $VMMData[2] -CustomPropertyScope $VMMData[0] -CustomPropertyName $VMMData[3]
}

Calling the Remove-VMMCustomProperty Workflow/Runbook

001
002
003
004
005
006
007
008
<# Calling Remove-VMMCustomProperty #>
$VMMCPRemoveData = @("Cloud:Startup Priority Groups","VM:Startup Group")

foreach($VMM in $VMMCPRemoveData) {
   
    $VMMData = $VMM.Split(":")
    Remove-VMMCustomProperty -Creds $Creds -VMMServer $VMMServer -CustomPropertyScope $VMMData[0] -CustomPropertyName $VMMData[1]
}

Download the SMA Runbooks / PowerShell v3 Workflow Examples

As promised, each of the above examples have been included in a TechNet Gallery Contribution.

The download includes the following (5) PS1 files which represent the (5) examples from the “Example SMA Runbook Rundown!” section above:

  • Create-VMMCustomProperty.ps1
  • Update-VMMCustomPropertyValue.ps1
  • Get-VMMCustomPropertyValue.ps1
  • Remove-VMMCustomPropertyValue.ps1
  • Remove-VMMCustomProperty.ps1

And the following (1) PS1 file which represents a collection of all the PowerShell snippets from the “Calling the Runbooks” section above:

  • Execute-VMMCustomPropertyRunbooks.ps1

Note     Each of the sections in this collection of PowerShell snippets has been individually commented out (including the variables at the top). To leverage any of the snippets, simply modify the comment tags.

Download the SMA Example - VMM Custom Property Management (w/PS v3 Workflow) from TechNet Gallery here:

BC-DLButtonDark


That’s it! I hope this VMM Custom Property Management Examples post provided you with a few more useful gadgets in your SMA Runbook / PowerShell v3 Workflow utility belt!

Oh, and if you want more examples for SMA and PowerShell v3 Workflow, be sure to check out this list (to be updated as more examples are provided):

And for more information, tips/tricks, and example solutions for SMA, be sure to watch for future blog posts in the Automation Track!

 

enJOY!