Storing and Running Scripts in VMM

 

Updated: May 13, 2016

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

A script is a plain text file that contains one or more Windows PowerShell commands. After you have written a script using the appropriate Windows PowerShell and System Center 2012 – Virtual Machine Manager cmdlets, you can save your script to a local directory and run it from the VMM command shell, or you can save your script to the VMM library and run it by using the VMM console. Save your script with .ps1 file name extension for it to be recognized as a Windows PowerShell script.

Running a Script by using the VMM Command Shell

If you save your script to a local directory, you can run the script from the VMM command shell by typing the path to the directory where the script resides, or, if your script is located in the current directory, you can use a dot (".") to represent the current directory, followed by a backslash ("\"). For example, to run a script called MyScript01.ps1 from the current directory, type the following at the command prompt: .\MyScript01.ps1. If your script is not in the current directory, then you need to type the path to the script. For example: C:\VMMScripts\MyScript01.ps1.

Note

Before you can run a script, you need to change the default Windows PowerShell execution policy. The default execution policy, "Restricted", prevents all scripts from running, including scripts that you write on the local computer. To change the execution policy, start a Windows PowerShell session with Administrator privileges and use the Set-ExecutionPolicy cmdlet. For more information, see about_Execution_Policies.

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

Storing Scripts in and Running Scripts from the VMM Library

Storing your scripts in a centralized location and sharing them with other VMM administrators is made easier by using the VMM library. You can store, view, update, and run your scripts directly from the library.

To store a script in the VMM library

  1. From the VMM console, navigate to the Library workspace, select the library share where you want to store your script, select a script in that share, and then on the PowerShell tab, in the Window group, click Open File Location. This opens a Windows Explorer window to the library share.

  2. Copy your script to the share and then close the Explorer window.

  3. The new script will become available in the library during the next library refresh. Or, you can perform the Refresh action in the VMM console, or run the Read-SCLibraryShare cmdlet in the VMM command shell to manually refresh the library and make the script available sooner.

To view and update a script in the VMM library

  1. From the VMM console, navigate to the Library workspace, select the script, and then on the PowerShell tab, in the Window group, click View File. Notepad opens with your script displayed.

  2. Make any desired updates to your script, then save and close the file. When the library next updates, you will see an updated date and time in the Date modified field under General Information.

To Run a script from the VMM library

  1. From the VMM console, navigate to the Library workspace, select the script, and then on the PowerShell tab, in the Actions group, click Run. The VMM command shell opens and your script runs.

See Also

VMM Cmdlet Help Topics