Deploying VMM 2012 Service Templates with Powershell

One of the most exciting capabilities in System Center 2012 is the ability to deploy applications using Service Templates. Service templates allow us to define a logical model of an application and its infrastructure requirements. Once we have a Service Template defined in Virtual Machine Manager 2012 (VMM 2012) we can assign who has access deploy it to into a Private Cloud. We typically manually deploy a Service Template via App Controller or the VMM 2012 Console. But what if we want to automate the deployment? We can accomplish this using the VMM 2012 PowerShell Cmdlets. 

My goal in this post is to provide you with a basic walkthrough of the Cmdlets and sequence needed to deploy a Service Template in VMM 2012 with PowerShell. To illustrate this process, I’m using the Stocktrader sample application and Service Template. I’m running VMM 2012 RC and I have the Stocktrader sample Service Template imported into my library. My examples do not include error checking and validation that you would want to add in an operational environment. 

Let’s jump into the example:

# We need the VMM Server, Release and Cloud as inputs

param([string]$VMMServer, [string]$Release, [string]$CloudName)

# Check to see if the VMM module is loaded

$ImportCheck = Get-Module|where { $_.Name -eq "virtualmachinemanager" }

if (-not $ImportCheck){import-module virtualmachinemanager}

# We set the static variables we need.  

$ServiceTemplateName=" Stocktrader Service "

$ServiceConfigName = " Stocktrader Service "

$SQLTierConfiguration="*SQL*"

$WEBTierConfiguration="*WEB*"

$OPTierConfiguration="*OP*"

$BSLTierConfiguration="*BSL*"

$ParameterList=@()

$ParameterList+=@{Name="configadmin ";Value="configadmin "}

$ParameterList+=@{Name="configadminPassword ";Value="Passw0rd "}

# Connect to the VMM server

$VMMServerObj=Get-SCVMMServer -ComputerName $VMMServer

# First we need to set a variable that references our cloud object.  

# We use Get-SCCloud to reference the Cloud object that we passed into the script.

$Cloud = Get-SCCloud -Name $CloudName

# We need to find the correct version of the Service Template that we want to deploy.

# We use Get-SCServiceTemplate to find the Stocktrader Service with the correct version number.

$ServiceTemplate = Get-SCServiceTemplate -Name "Stocktrader Service"| where { $_.Release -match $Release }

Get-SCServiceConfiguration -Name $ServiceConfigName | Remove-SCServiceConfiguration

# The next set of commands creates a new Service Configuration object.  

# By default the Service Configuration will only exist temporarily until the deployment is complete.  

$ServiceConfig = New-SCServiceConfiguration -ServiceTemplate $ServiceTemplate -Name $ServiceConfigName -Cloud $Cloud -ServicePriority High

# We use Update-SCServiceConfiguration to perform placement on the configuration

#If placement fails we stop executing the script

$ServiceUpdate = Update-SCServiceConfiguration -VMMServer $VMMServer -ServiceConfiguration $ServiceConfig

if($sc.deploymenterrorlist -ne $null)

{

Write-Host "Cloud Placement failed for Service Deployment..."

                break

}

# The Stocktrader example requires us to define configuration settings that will be used to deploy an instance of the service.

# Each application tier needs knowledge of how to communicate to other applications tiers. We also need to define a configuration admin account.

# The next set of commands sets each ComputerTierConfiguration setting based on the ServiceConfiguration ComputerName generated by the placement engine.  

$ConfigValue = Get-SCVMConfiguration -ServiceConfiguration $ServiceConfig | where { $_.ComputerTierConfiguration -like $SQLTierConfiguration}

Get-SCServiceSetting -ServiceConfiguration $ServiceConfig -Name "sqlComputerName" |Set-SCServiceSetting -value $ConfigValue.ComputerName

$ConfigValue = Get-SCVMConfiguration -ServiceConfiguration $ServiceConfig | where { $_.ComputerTierConfiguration -like $WEBTierConfiguration}

Get-SCServiceSetting -ServiceConfiguration $ServiceConfig -Name "rootWebMachineName" |Set-SCServiceSetting -value $ConfigValue.ComputerName

$ConfigValue = Get-SCVMConfiguration -ServiceConfiguration $ServiceConfig | where { $_.ComputerTierConfiguration -like $OPTierConfiguration}

Get-SCServiceSetting -ServiceConfiguration $ServiceConfig -Name "rootOPMachineName" |Set-SCServiceSetting -value $ConfigValue.ComputerName

$ConfigValue = Get-SCVMConfiguration -ServiceConfiguration $ServiceConfig | where { $_.ComputerTierConfiguration -like $BSLTierConfiguration}

Get-SCServiceSetting -ServiceConfiguration $ServiceConfig -Name "rootBSLMachineName" |Set-SCServiceSetting -value $ConfigValue.ComputerName

# Set the admin user/pass info

foreach($Parameter in $ParameterList)

{

     Get-SCServiceSetting -ServiceConfiguration $ServiceConfig -Name $Parameter.Name |Set-SCServiceSetting -value $Parameter.Value

}

# We are ready to create an instance of the service.

# We use New-SCService to pass in the ServiceConfiguration and VMM Server.

$NewService = New-SCService -VMMServer $VMMServer -ServiceConfiguration $ServiceConfig

 

Once I save this script to a file I can run it directly from a Powershell prompt to execute:

                PS c:\stocktraderdeploy.ps1–VMMServer “vmm01” –Release “1” –CloudName “Production”

 

Now I have a deployed version 1 of the Stocktrader Service in my Production Cloud!

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included utilities are subject to the terms specified at http://www.microsoft.com/info/copyright.htm.