Converting Cmdlet Help Examples into Scripts

Letzte Aktualisierung: Oktober 2008

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

The Help for the Windows PowerShell – Virtual Machine Manager command shell includes hundreds of examples of System Center Virtual Machine Manager (VMM) commands that you can type in the command shell. Many of these examples contain multiple lines of code, and the examples can be saved as scripts.

Converting an Example for Restart-Job into a Script

The following procedure demonstrates how to convert an existing example in the VMM command shell Help into a script. The script restarts canceled VMM jobs.

To convert an existing cmdlet Help example into a script

  1. Click Start, click All Programs, click Microsoft System Center, click Virtual Machine Manager 2008, and then click Windows PowerShell - Virtual Machine Manager.

  2. Type a command to change to the directory where you want to save the script. For example, type a command that resembles the following command:

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

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

    notepad RestartCancelledJobs.ps1

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

  5. At the top of the Notepad file, type or paste the following code comment:

    # File name: RestartCancelledJobs.ps1

  6. At the command prompt, type the following command to display the cmdlet Help examples for the Restart-Job cmdlet:

    Get-Help Restart-Job -example

  7. Read the explanatory text for example 1. The text describes how the commands restart all the jobs that were canceled on any virtual machine.

  8. Copy the code for example 1 to the Notepad file. To do this, follow these steps:

    1. Right-click inside the command-shell window, click Mark, select the code for example 3, and then press ENTER.

      Hinweis

      If the Mark command is not displayed when you right-click inside the command-shell window, click the icon in the upper left corner of the Windows PowerShell - Virtual Machine Manager title bar, click Edit, and then click Mark.

    2. In the open RestartCancelledJobs.ps1 file, put the cursor under the # File name: RestartCancelledJobs.ps1 file name comment, right-click, and then click Paste.

    3. Remove any hard returns from the middle of a line, and then delete the text that represents the Windows PowerShell command prompt (PS C:\>). (The command prompt indicates the start of each line of code). The content of the RestartCancelledJobs.ps1 file now appears as follows.

      # Filename: RestartCancelledJobs.ps1
      Get-VMMServer -ComputerName "<VMMServer1>.<Contoso>.com"
      Get-Job | where { $_.ResultName -eq "job canceled" -and $_.Status -eq "Canceled" } | Restart-Job
      
  9. Substitute the names that you use in your environment for the placeholder names that are used in the cmdlet Help. In this example, substitute your VMM server name for the <VMMServer1> placeholder. Substitute your domain name for the <Contoso>.com placeholder.

  10. Save and then close the RestartCancelledJobs.ps1 file.

  11. Run the script.

    Hinweis

    For information about how to run scripts, see Running Scripts.