Creating a Script from VMM Wizards and Property Pages

 

Updated: May 13, 2016

Applies To: System Center 2012 R2 Virtual Machine Manager, System Center 2012 - Virtual Machine Manager

To help get you started with writing scripts, in System Center 2012 – Virtual Machine Manager you can generate a script when you run a wizard or update properties in the VMM console. You can use the View Script button on the Summary page of each wizard in the VMM console or Properties page to view the set of cmdlets that will run when you click Finish at the end of a wizard or OK when you update properties. You can modify the commands as necessary and then save the file with a .ps1 extension, which identifies it as a Windows PowerShell script.

Modifying the Generated Script

Most generated scripts use the ID parameter of cmdlets to identify specific objects on which they will take actions. Because this ID is specific to that object, you may want to allow your script to take parameters or use a Get cmdlet to get an object to make your script available for use with other objects.

Adding a Get cmdlet to a Generated Script

When you make a change to a property value, the generated script uses an ID to get the object to change. For example, the following script is generated when changing the name and description for a logical network:

$logicalNetwork = Get-SCLogicalNetwork -ID "7a858ed4-b8d2-4ac8-9dbe-6e6a4388c1e7"  
Set-SCLogicalNetwork -Name "Backend" -Description "Backend logical network for Seattle" -LogicalNetwork $logicalNetwork -RunAsynchronously  

To make this script useful in additional environments, you can replace the ID parameter with a where clause. For example:

$logicalNetwork = @(Get-SCLogicalNetwork | where { $_.Name -like "LogicalNet*" })  
Set-SCLogicalNetwork -Name "Backend" -Description "Backend logical network for Seattle" -LogicalNetwork $logicalNetwork[0] -RunAsynchronously  

The first command now retrieves all logical networks that have a name that begins with "LogicalNet" and places them in an array. The second command modifies the first logical network in the array, updating its name to "Backend".

Adding Parameters to a Generated Script

Another way to make a generated script more accessible is to define parameters in the script. Script parameters work like function parameters. The parameter values are available to all of the commands in the script. When running the script, script users type the parameters after the script name.

To add parameters to a script, use the Param statement. The Param statement must be the first statement in the script except for comments. The following example shows the generated script from above modified so that the name of an existing logical network and a new name for the logical network can be designated, and a description added, when the script is run.

  
Param(  
   [parameter(Mandatory=$true)]  
   [String] $LogicalNetwork = $(throw "A name for an existing logical network is required."),  
  
   [parameter(Mandatory=$true)]  
   [String] $NewName = $(throw "A new name for the logical network is required."),  
  
   [parameter(Mandatory=$false)]  
   [String] $Description  
   )  
Set-SCLogicalNetwork -Name $NewName -Description $Description -LogicalNetwork $LogicalNetwork -RunAsynchronously  
  

When you run this script, you must provide the name of an existing logical network and a new name for the logical network. Providing a description is optional. For example if this script is saved as "UpdateLogicalNetworkName.ps1", you would type the following at the command prompt: .\UpdateLogicalNetworkName.ps1 -LogicalNetwork "LogicalNetwork01" -NewName "Backend" -Description "Backend logical network for Seattle." If you don't provide values for LogicalNetwork and NewName, you will be prompted for them.

For more information about writing Windows PowerShell scripts, see about_Scripts.

See Also

VMM Cmdlet Help Topics