Providing User Credentials in a Script

Letzte Aktualisierung: Oktober 2008

Betrifft: Virtual Machine Manager 2008, Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

To perform certain common actions in System Center Virtual Machine Manager (VMM), you must provide user credentials. For example, you must provide your user credentials when you add new host servers on which you want to deploy virtual machines. You can provide credentials in a single script, or you can provide credentials so that the credentials are available to all the scripts that you run in the same session.

Storing Credentials

When you type the Windows PowerShell Get-Credential cmdlet at the Windows PowerShell command prompt, Windows PowerShell prompts you to enter a user name and password. After you type a user name and password, Windows PowerShell stores these credentials in a Windows PowerShell credential object (a PSCredential object). If you want to avoid being prompted each time a script requires user credentials, you can use the $Credential = GetCredential command to store the credentials in a secure manner in a variable that the script can then reuse. In this case, you are prompted only one time for the credentials.

Warnung

It is possible to produce a PSCredential object programmatically through Windows PowerShell without requiring user interaction. However, this method requires that the user name and password appear in plain text in the script. Therefore, we do not recommend this method.

Providing Credentials in a Script That Adds Hosts

The following procedure demonstrates how to store credentials for an account that has permissions to add hosts. In this procedure, you create a VMM script that adds a list of servers as hosts to VMM for virtual machines.

To create a script that adds a list of servers as hosts to Virtual Machine Manager

  1. In Windows PowerShell, change to the directory in which you want to save the script. Type a command such as the following command:

    Set-Location 'C:\Users\<username>\Documents\MyScripts'

  2. Type the following command to create and name the script:

    notepad AddVMHostsList.ps1

  3. When you are prompted to create a new file, click Yes.

  4. Type or copy the following series of comments and commands to create the script.

    Hinweis

    Substitute your actual server and domain names for the <VMHost01>.<Contoso.com>, <VMHost02>.<Contoso.com>, and <VMMServer1><Contoso.com> placeholders. Additionally, type each command on a single line even though the longer commands are shown here on multiple lines for readability.

    # Filename: AddVMHostsList.ps1
    # Display a blank line.
    Write-Host
    
    # Send a message to the screen.
    Write-Host -backgroundColor Red "When the Windows PowerShell Credential Request dialog box opens, type the user name and password for an account that has permission to add a host."
    
    # Store credentials that have permissions to add hosts in a variable.
    $Credential = Get-Credential
    
    # Store host names in a variable.
    $ServerNames = "<VMHost01>.<Contoso.com>", "<VMHost02>.<Contoso.com>"
    
    # Add each server as a host.
    foreach( $ServerName in $ServerNames ) { Add-VMHost -VMMServer <VMMServer1>.<Contoso.com> -ComputerName $ServerName -Credential $Credential }
    
    # Display a blank line.
    Write-Host
    # End of script
    
  5. Save the file as AddVMHostList.ps1, and then close the file.

  6. At the command prompt, type the following command, and then press ENTER:

    .\AddVMHostsList.ps1

  7. When you are prompted to provide a user name and password, type the credentials of an account that has permissions to add hosts to the domain.

  8. Confirm that the script added the two specified hosts. To do this, verify that the object for each server is returned as each host is added. Or, open the VMM Administrator Console to confirm that the script added the hosts.

Storing Credentials that All the Scripts in a Session Can Use

If you run multiple scripts that require credentials, you can use the following command to store the PSCredential object in a global variable that all scripts can use:

$global:Credential = Get-Credential

When you use this command, you enter the required credentials only at the start of a session. During that session, these credentials are provided for all the scripts that you run that use the $Credential variable.