Starting and stopping virtual machines

You have created your virtual machine, but it is still powered off. In this section, you will learn how to start, suspend, and stop a virtual machine using PowerCLI.

Starting virtual machines

To start a virtual machine, you can use the Start-VM cmdlet. This cmdlet has the following syntax:

Start-VM [-RunAsync] [-VM] <VirtualMachine[]> [-Server 
    <VIServer[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

The -VM parameter is required to start a virtual machine.

In the first example, we will start the virtual machine VM2 using the following command:

PowerCLI C:> Start-VM -VM VM2

The output of the preceding command is as follows:

Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
VM2                  PoweredOn  2        4.000

To start all of your virtual machines that are powered off, you can pipe the output of the Get-VM cmdlet to the Where-Object cmdlet, to filter only those virtual machines that are powered off. Pipe the result to the Start-VM cmdlet using the following command and all of your virtual machines that are powered off will be started:

PowerCLI C:> Get-VM |
>> Where-Object {$_.PowerState -eq 'PoweredOff'} | Start-VM
>>

The output of the preceding command is as follows:

Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
VM5                  PoweredOn  1        0.250
VM4                  PoweredOn  1        0.250
VM3                  PoweredOn  1        0.250 

Suspending virtual machines

If you want to put a virtual machine on hold without powering it down, you can suspend it using the Suspend-VM cmdlet. This cmdlet has the following syntax:

Suspend-VM [-RunAsync] [-VM] <VirtualMachine[]> [-Server
    <VIServer[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

The -VM parameter is required.

In the following example, we will suspend the VM4 virtual machine:

PowerCLI C:> Get-VM -Name VM4 | Suspend-VM -Confirm:$false

The output of the preceding command is as follows:

Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
VM4                  Suspended  1        0.250

If you want to unsuspend a virtual machine, you have to use the Start-VM cmdlet.

Shutting down the virtual machine's guest operating systems

If you want to shut down the operating system of a virtual machine, you should use the Stop-VMGuest cmdlet. This cmdlet uses VMware Tools to ask the guest operating system to perform a graceful shutdown. If VMware Tools is not running in the virtual machine, the Stop-VMGuest cmdlet will not work and will return an error message. The cmdlet returns immediately and does not wait until the guest operating system has been shut down. The Stop-VMGuest cmdlet has the following syntax:

Stop-VMGuest [[-VM] <VirtualMachine[]>] [[-Server]
    <VIServer[]>] [-WhatIf] [-Confirm] [<CommonParameters>]
Stop-VMGuest [[-Guest] <VMGuest[]>] [-WhatIf] [-Confirm] 
    [<CommonParameters>]

You must specify either the -Guest or -VM parameter.

In the following example, we will shut down the guest operating system of the VM4 virtual machine:

PowerCLI C:> Stop-VMGuest -VM VM4 -Confirm:$false

The output of the preceding command is as follows:

State    IPAddress            OSFullName
-----    ---------            ----------
Running  {192.168.0.145, f... Microsoft Windows Server 2016 (64-bit)

Stopping virtual machines

If shutting down a virtual machine is not possible, because VMware Tools is not installed on the guest operating system, you can use the Stop-VM cmdlet to stop a virtual machine. The Stop-VM cmdlet works similarly to the power switch on a physical computer. This will force the virtual machine to power off without gracefully shutting down the guest operating system. The syntax of the Stop-VM cmdlet is as follows:

Stop-VM [-Kill] [-RunAsync] [-VM] <VirtualMachine[]> [-Server 
    <VIServer[]>] [-WhatIf] [-Confirm] [<CommonParameters>]

The -VM parameter is required.

In the following example, we will stop the VM3 virtual machine:

PowerCLI C:> Stop-VM -VM VM3 -Confirm:$False

The output of the preceding command is as follows:

Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
VM3                  PoweredOff 1        0.250
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.37.68