Sample Scripts for Managing Snap-ins

You can experiment with the Windows PowerShell scripts that are described in this topic to learn how to automate adding one or more Windows PowerShell snap-ins to the command shell.

If you open the Windows PowerShell – Virtual Machine Manager command shell, you can run both standard Windows PowerShell cmdlets and Virtual Machine Manager cmdlets. However, if you open the standard Windows PowerShell command shell, the Virtual Machine Manager cmdlets are not available unless you add the Virtual Machine Manager snap-in to the standard shell.

In some cases, you can add multiple snap-ins to the standard Windows PowerShell command shell if you want to be able to view and use cmdlets for two or more Microsoft products in the same shell. In other cases, adding multiple snap-ins to the standard Windows PowerShell command shell will not work. For example, if you want to access Virtual Machine Manager cmdlets in the same command shell as System Center Operations Manager, the best way to accomplish it is to add the path for the Windows PowerShell console file for Virtual Machine Manager to the Windows PowerShell console file for System Center Operations Manager.

AddVMMSnapin.ps1

You can use the following script to automate adding the Virtual Machine Manager snap-in to the standard Windows PowerShell command shell. Copy the following complete version of AddVMMSnapin.ps1 into a Notepad file and save it as AddVMMSnapin.ps1.

# Filename:    AddVMMSnapin.ps1
# Description: Adds the Virtual Machine Manager snap-in to the 
#              standard Windows PowerShell command shell. 
#              For this script to run successfully, Virtual Machine 
#              Manager must be installed on the server on which you 
#              run this script.
# DISCLAIMER:
# Copyright (c) Microsoft Corporation. All rights reserved. This 
# script is made available to you without any express, implied or 
# statutory warranty, not even the implied warranty of 
# merchantability or fitness for a particular purpose, or the 
# warranty of title or non-infringement. The entire risk of the 
# use or the results from the use of this script remains with you.
######################################################################
# Add the VMM snap-in to the standard Windows PowerShell command shell.
######################################################################
Add-PSSnapin -name VirtualMachineManagerSnapIn

AddVMMExchSnapins.ps1

If you also want to run Windows PowerShell cmdlets for other Microsoft products, such as Microsoft Exchange Server, in the same command shell as the Virtual Machine Manager cmdlets, you must add the snap-ins for Virtual Machine Manager and the other products to the generic Windows PowerShell command shell.

Before You Start

You cannot add a snap-in that contains Windows PowerShell cmdlets for a particular Microsoft product to a server unless that product is installed on that server and a Windows PowerShell snap-in exists for that product.

To determine which Windows PowerShell snap-ins you can add on a server

  1. On a server on which Virtual Machine Manager and Microsoft Exchange Server are installed, click Start, point to All Programs, point to Windows PowerShell 1.0, and then click Windows PowerShell.

    Note

    Do not click Start, point to All Programs, point to Microsoft System Center Virtual Machine Manager 2007, and then click Windows PowerShell – Virtual Machine Manager. You cannot add snap-ins for other Microsoft products to the Windows PowerShell – Virtual Machine Manager command shell.

  2. To determine which snap-ins you can add to the standard Windows PowerShell console, type the following command at the command prompt:

    Get-PSSnapin -Registered
    

    This example assumes that Virtual Machine Manager and Microsoft Exchange Server are installed on the server, so the output should list a Windows PowerShell snap-in for each of these server products.

Add-VMMExchSnapins.ps1 - Complete Script

You can use the following script to automate adding the Virtual Machine Manager and Microsoft Exchange Server snap-ins to the standard Windows PowerShell command shell. Copy the following complete version of AddVMMExchSnapins.ps1 into a Notepad file and save it as AddVMMExchSnapins.ps1.

# Filename:    AddVMMExchSnapins.ps1
# Description: Adds two snap-ins to the standard Windows PowerShell 
#              command shell: Virtual Machine Manager, and Microsoft 
#              Exchange Server. For this script to run 
#              successfully, both  server products must be installed 
#              on the server on which you run this script.
# DISCLAIMER:
# Copyright (c) Microsoft Corporation. All rights reserved. This 
# script is made available to you without any express, implied or 
# statutory warranty, not even the implied warranty of 
# merchantability or fitness for a particular purpose, or the 
# warranty of title or non-infringement. The entire risk of the 
# use or the results from the use of this script remains with you.
######################################################################
# Add the VMM and Exchange snap-ins to the standard Windows 
# PowerShell command shell.
######################################################################
Add-PSSnapIn -Name VirtualMachineManagerSnapIn, Microsoft.Exchange.Management.PowerShell.Admin

AddVMMCmdletsToOpsMgrConsole.ps1

If you want to make both Virtual Machine Manager cmdlets and System Center Operations Manager cmdlets available in the same Windows PowerShell window, you cannot use the Windows PowerShell cmdlet Add-PSSnapIn as described in the preceding section. If you did so, you would lose Operations Manager functionality for two reasons:

  • The Operations Manager implementation of Windows PowerShell includes a provider in addition to cmdlets, but the Virtual Machine Manager implementation of Windows PowerShell includes only cmdlets.
  • When you open Windows PowerShell from Operations Manager, Operations Manager loads its own Windows PowerShell console, and it also loads its own custom startup script.

You can use the following procedure to confirm that Operations Manager runs a custom startup script when you open the Operations Manager version of the command shell.

To confirm that Operations Manager runs a custom startup script when you open its console

  1. On a server on which Operations Manager is installed, click Start, point to All Programs, point to System Center Operations Manager 2007, right-click Command Shell, and then click Properties.

  2. View the contents of the Target field:

    <C>:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Console.psc1 -NoExit .\Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Startup.ps1

    Notice that, after loading the Windows PowerShell console for Operations Manager, the command in the Target field also runs the custom script called Startup.ps1.

You can accommodate the Operations Manager provider and its startup script and you can also make Virtual Machine Manager cmdlets available by adding the Virtual Machine Manager cmdlets to the Operations Manager console. You can use the following procedure to make Machine Manager cmdlets available in the Operations Manager command shell.

To add Virtual Machine Manager cmdlets to the Operations Manager console

  1. On a computer on which both Virtual Machine Manager and Operations Manager are installed, open Windows Explorer and navigate to:

    C:\Program Files\System Center Operations Manager 2007

  2. Open Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Console.psc1 in Notepad.

  3. Confirm that the original contents of the .psc1 console script file appear as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <PSConsoleFile ConsoleSchemaVersion="1.0">
      <PSVersion>1.0</PSVersion>
      <PSSnapIns>
        <PSSnapIn Name="Microsoft.EnterpriseManagement.OperationsManager.Client" />
      </PSSnapIns>
    </PSConsoleFile>
    
  4. Add <PSSnapIn Name="VirtualMachineManagerSnapIn" />, which loads the Virtual Machine Manager cmdlets, to Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Console.psc1 to , so that the Operations Manager . psc1 console script file appears as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <PSConsoleFile ConsoleSchemaVersion="1.0">
      <PSVersion>1.0</PSVersion>
      <PSSnapIns>
        <PSSnapIn Name="Microsoft.EnterpriseManagement.OperationsManager.Client" />
        <PSSnapIn Name="VirtualMachineManagerSnapIn" />
      </PSSnapIns>
    </PSConsoleFile>
    
  5. Save and close Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Console.psc1.