Providing Credentials in a VMM Script
Updated: May 13, 2016
Applies To: System Center 2012 R2 Virtual Machine Manager, System Center 2012 - Virtual Machine Manager
To perform certain actions in System Center 2012 – Virtual Machine Manager (VMM), you must provide credentials. For example, you must provide credentials that have the proper administrator permissions to add a virtual machine host to VMM management. To accomplish this, you can create a Windows PowerShell PSCredential object, and store that object in a variable that you can use within a script. Additionally, System Center 2012 – Virtual Machine Manager introduces the concept of Run As accounts that you can also use within scripts to provide appropriate credentials to perform an action.
Using the PSCredential Object
To create a PSCredential object, use the Get-Credential cmdlet. When you type Get-Credential at the command prompt, or use it in a script, the user is prompted to enter a user name and password with which a PSCredential object is created. You can then store that object in a variable which you can use throughout a script where those credentials are needed.
The following example illustrates how to create a PSCredential object that you can store and use to add a virtual machine host:
$Credential = Get-Credential
$Add-SCVMHost "VMHost01.Contoso.com" -Description "This is a new host" -RemoteConnectEnabled $True -RemoteConnectPort 5900 -Credential $Credential
Warning
You can create a PSCredential object programmatically without requiring user interaction. However, this method requires that the user name and password appear in plain text within the script. Therefore, this method is not recommended.
Using VMM Run As accounts
In System Center 2012 – Virtual Machine Manager, you can now use Run As accounts to provide credentials for cmdlets that support the VMMCredential parameter type. Credentials stored in VMM are encrypted and then stored in the VMM database. To create a Run As account, use the New-SCRunAsAccount cmdlet and provide a user account that has permissions to accomplish the tasks for which you want to use the Run As account. For more information about creating a Run As account, see New-SCRunAsAccount.
After you create the Run As account, you can then use the Get-SCRunAsAccount cmdlet to get your Run As account within a script, which provides credentials without the need for user input. An administrator can also allow Self-Service users or user roles to use the Run As account by using the Set-SCRunAsAccount cmdlet. For more information about Run As accounts in System Center 2012 – Virtual Machine Manager, see about_VMM_2012_Run_As_Accounts and Configuring Run As Accounts in VMM.
The following example illustrates how to add a virtual machine host by providing a Run As account:
$RunAsAccount = Get-SCRunAsAccount -Name "RunAsAcct01"
$Add-SCVMHost "VMHost01.Contoso.com" -Description "This is a new host" -RemoteConnectEnabled $True -RemoteConnectPort 5900 -Credential $RunAsAccount
See Also
New-SCRunAsAccount
Get-SCRunAsAccount
about_VMM_2012_Run_As_Accounts