When it comes to the bulk management of Hyper-V hosts (or of any Windows server, for that matter), I have always gravitated toward the use of PowerShell. Even so, using PowerShell to manage a collection of hosts tends to involve more coding than whatever happens to be required for the management activity you are trying to perform. Typically, for instance, you will need a list of the servers to be managed, and you will need some sort of looping structure that performs the management task against each server on the list. Although this method of using PowerShell to manage remote hosts works really well, there is a way to simplify things a bit. If you have System Center Virtual Machine Manager (VMM), you can use its management console as a tool for running a script against one or more hosts, without the hassle of writing code to loop through the list of hosts. Incidentally, you aren’t limited to working solely with PowerShell. You can also launch scripts from the Windows command prompt. Let’s take a look at how it’s done.
To get started, open the VMM Management Console, and then select the VMs and Services workspace. Next, expand a host group, and click on the host that you want to manage. When you do, you will see a Host tab appear on the toolbar at the top of the screen. As you can see in the image below, this toolbar contains an option to run a script command.
Upon clicking on Run Script Command, you will be taken to the Run Script Command dialog box, which you can see in the image below.
As you can see, the Run Script Command dialog box includes several fields. One of the first fields that you need to be familiar with is the Executable Program field. While it may be tempting to populate this field with the name of your script, this field is actually used to launch the scripting environment.
Based on the documentation that I have found, it seems that Microsoft has intended for this field to be used exclusively with the Windows Command Prompt environment (CMD.EXE). It is possible to run PowerShell scripts, but you have to jump through a couple of hoops to make it happen. As such, I need to first familiarize you with running Command prompt commands, and then I will show you how to adapt the procedure to support PowerShell.
Running Windows command prompt commands
As I have spent time working with the Run Script Command dialog box, I have found that it is extremely picky when it comes to the command syntax that you use. That’s why I want to begin by showing you how to execute a command prompt command.
It seems that four elements absolutely must be present to make a command prompt command execute properly. The first of these elements is that as previously noted, the Executable program must be set to cmd.exe. The .EXE portion of the command is important because the command will fail without it.
The second element that you must have in place is a /Q in the Parameters field. The /Q parameter tells Windows to run the Command Prompt environment in “echo off” mode. In case you are wondering, echo off was an old DOS command that hid certain output from view.
The third required element is the /C switch, which must also be expressed in the Parameters field. This switch tells the Command Prompt window to run the command and then terminate. If you omit this switch, then the job associated with the script will hang.
The fourth required element is the actual command that you want to run within the Windows command prompt environment. The example that Microsoft gives in its documentation is:
Net start sftlist
In case you are wondering, this command starts the sftlist service. You can see what the fully populated Run Script Command dialog box looks like in the screenshot below:
Running PowerShell scripts
Although the Microsoft documentation does not say anything about running PowerShell scripts through the Run Script Command dialog box, I have found a way to do it. Again, the dialog box is picky about the syntax that you use.
Like before, you will need to enter cmd.exe as the executable program name, and you will have to include /q and /c at the beginning of the Parameters field.
The next step is to type powershell.exe in the list of parameters. Again, you must include the .exe portion of the command, or PowerShell will fail to launch.
Finally, you will need to append the -File parameter, followed by the path and filename of the script that you want to test.
When I was testing this procedure out, I created a script called copy.ps1 that simply copied a file from one folder to another (so that I could verify that the script was indeed executing). The image below shows the commands that I used to launch the script.
How do I know if the VMM script worked?
When you use VMM to execute a script, you won’t see the scripting environment visibly open on the remote host server. The best way to find out if VMM successfully launched the script or not is to take a look at the Jobs workspace. The image below shows some examples of some successful jobs and some failed jobs. You will also notice that there are a few canceled jobs. I had to cancel these particular jobs because omitting the /Q and /C switches caused the jobs to hang. In any case, it can sometimes be tricky to come up with just the right syntax for running a script, and the Jobs menu can help you to refine your technique and to confirm whether or not your VMM script ran.
Featured image: Designed by Freepik