Monitor Hyper-V with the command line (Part 3): Check virtual machine status

The previous article showed you how to turn on and off the virtual machine with the command line. This article will introduce two methods of validating that virtual machines are running and responding.

TipsMake.com - The previous article showed you how to turn on and off the virtual machine with the command line. This article will introduce two methods of validating that virtual machines are running and responding.

Note that the user commands do not belong to Hyper-V. They are in the Hyper-V monitoring library. moreover, this library must be imported into PowerShell each time you plan to use any Hyper-V related commands. The library entry statement is:

Import-Module 'C: Program FilesModulesHypervHyperv.psd1'

The full instructions on how to download and install the Hyper-V monitoring library are provided in Part 1 of this series.

Error finding commands

Administrators often depend on the ping command to test the server's connection. Very simple. Perform a ping command to the machine and see if it responds. Although the ping command works relatively well, there is a command in PowerShell called Ping-VM that is also specifically designed to ping virtual machines.

First of all, the Ping-VM command seems redundant and the syntax is a bit more complicated than the usual ping command, but the command has its own advantages. The ping-VM command requires the user to provide the virtual machine name to be pinged and the name of the host server. For example, if we want to ping a virtual machine called Lab-DC that is running on a name server called Hyper-V, the command structure will be:

Ping-VM 'Lab-DC' –Server Hyper-V

When this command is executed, the Ping-VM command uses the integrated services on the virtual machine to find Fully Qualified Domain Name and then it performs an ICMP ping command.

There are two reasons to use Ping-VM instead of Ping. First, Ping-VM recognizes the virtual machine. Second, Ping-VM can run multiple virtual machines using wildcards. To illustrate these concepts, let's look at the picture below. The host server has several virtual machines (Lab-). We will execute the order for these virtual machines. Ping-VM returned information on 6 virtual machines and could even determine which virtual machines were turned off.

Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 1Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 1

You can also use Ping-VM to check the status of virtual machines located on host machines. To do this, simply replace the virtual machine name with an asterisk. You can even do more and display a warning message for any virtual machine running, but not ping. The statement is:

Get-VM -r | foreach-object {if ((Ping-VM $ _). StatusCode –ne 0) {'$ ($ _. elementname) is inaccessible'}}

The command does not output if the virtual machine is successfully pinged.

In addition, the Ping-VM command is also used to ping virtual machines on multiple Hyper-V hosts. Just give the name of each host you want to ping. Machine names must be separated by commas. For example, if you want to ping all virtual machines on Hyper-V1, Hyper-V2 and Hyper-V 3, the command to use is:

Ping-VM * -Server Hyper-V1, Hyper-V2, Hyper-V3

Check virtual machine status

Not only can Ping-VM be used to see if a virtual machine responds, users can also use the Test-VmHeartBeat command. Like Ping-VM, the Test-VMHeartBeat command depends on the integrated services running on the virtual machine tested.

The command syntax is relatively simple. The user must specify the virtual hostname and duration. Duration is calculated in seconds. The Test-VmHeartBeat command will check the status every 5 seconds until the specified time runs out. For example, If you want to check the status of the virtual machine for 5 minutes, set the time limit to 300. Observe the picture below to see more clearly for Lab-DC virtual machines.

Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 2Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 2

Like the Ping-VM command, users can use the Wildcard of the virtual machine name and assign multiple server servers.

Since Ping-VM and Test-VMHeartBeat are both used to test virtual machine responses, users can wonder which command to use.

If you want a quick status report from all virtual machines (even those that don't work), it's better to use Ping-VM. The reason is that Test-VMHeartBeat will block and wait for the time limit for each virtual machine that is currently disabled. For example, suppose the server server has 10 virtual machines, but only 5 of them are active now. We execute the following command:

Test-VmHeartBeat * -Timeout 300

The command will take up to 25 minutes to complete because the Test-VM command will wait for a full 5 minutes while checking each virtual machine is turned off. In contrast, the Ping-VM command will provide similar basic status information, but will complete almost immediately.

Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 3Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 3

However, Test-VmHeartBeat is especially useful when virtual machines need to be started in turn. For example, Lab-DC needs to be launched before Exchange Server (Lab-E2K10). Use the Test-VmHeartBeat command to check if Lab-DC is running before Exchange Server. To start these two servers sequentially, we can use the following command:

Start-vm 'Lab-DC'; Test-VmHeartBeat 'Lab-DC' -Timeout 300; Start-vm 'Lab-E2K10'

Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 4Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 4

In the image above, users can see PowerShell start Lab-DC and then stop to wait for the virtual machine status to be recorded. After Lab-DC works, Exchange Server is started as shown below.

Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 5Monitor Hyper-V with the command line (Part 3): Check virtual machine status Picture 5

Conclude

Now, you know how to check virtual machine status with the command line. In the next section, we will see some tricks for allocating memory for virtual machines.

4 ★ | 20 Vote