Automating/Scripting VMware VM operations in PowerShell

Connect to your VCenter/VMHost:

PowerCLI C:> Connect-VIServer VMHOST01

Specify Credential
Please specify server credential
User: root
Password for user root: ************

Name                           Port  User
—-                           —-  —-
VMHOST01                       443   root

PowerCLI C:>

 

Know your VMHost hypervisor version running:

PowerCLI C:> vmhost | select-object version

Version
——-
4.1.0

PowerCLI C:>

PowerCLI C:> Get-VMHost | % { (Get-View $_.ID).Config.Product }

Name                  : VMware ESXi
FullName              : VMware ESXi 4.1.0 build-260247
Vendor                : VMware, Inc.
Version               : 4.1.0
Build                 : 260247
LocaleVersion         : INTL
LocaleBuild           : 000
OsType                : vmnix-x86
ProductLineId         : embeddedEsx
ApiType               : HostAgent
ApiVersion            : 4.1
InstanceUuid          :
LicenseProductName    : VMware ESX Server
LicenseProductVersion : 4.0
DynamicType           :
DynamicProperty       :

PowerCLI C:>

 


 

Know the DataCenter name that your host is part of: (You’ll see the default value of “ha-datacenter” for a standalone VMhost, which is NOT part of any Datacenter or doesn’t have a vCenter server configured to manage it)

PowerCLI C:> Get-Datacenter

Name
—-
ha-datacenter

PowerCLI C:>

 

View List of VMs running on your HOST: (to keep the output tidy I am consolidating the result objects into their respective count)

 

PowerCLI C:> Get-VM -Server VMHOST01 | Measure-Object | Select-Object count | ft -auto

Count
—–
   33

PowerCLI C:> @(Get-VM -Server VMHOST01).count
33
PowerCLI C:>

 

Find the VM by name and know it’s current Power status that you want to do further manage:

PowerCLI C:> Get-VM -Name "*test*machine*"

Name                 PowerState Num CPUs MemoryGB
—-                 ———- ——– ——–
Test_Machine01       PoweredOff 1        4.000

PowerCLI C:>

 

Find the VM by name along with their IP-addresses:

 

PowerCLI C:> get-vm | % {$_.name, $_.guest.IPAddress[0]}

VMware vCenter Server Appliance

172.25.1.212

PowerCLI C:>

OR

PowerCLI C:> get-vm | select Name,@{N="IP Address";E={@($_.guest.IPAddress[0])}}

Name IP Address

—- ———-

VMware vCenter Server Appliance 172.25.1.212

PowerCLI C:>

 

Start/Power ON the VM:

PowerCLI C:> Start-VM -VM "Test_Machine01"

Name                 PowerState Num CPUs MemoryGB
—-                 ———- ——– ——–
Test_Machine01       PoweredOn  1        4.000

PowerCLI C:>

 

Stop/Power OFF the VM:

PowerCLI C:> Stop-VM -VM "Test_Machine01" -Confirm:$False

Name                 PowerState Num CPUs MemoryGB
—-                 ———- ——– ——–
Test_Machine01       PoweredOff 1        4.000

PowerCLI C:>

 

Create a new VM based on existing VM or Clone an existing VM:

(Note to run below command you need to connect to your vCenter server and then run)

 

PowerCLI C:> get-vm

Name PowerState Num CPUs MemoryGB

—- ———- ——– ——–

SUSE-SRV1 PoweredOn 1 0.0
47

VMware vCenter Se… PoweredOn 2 8.000

PowerCLI C:> get-datastore

Name FreeSpaceGB CapacityGB

—- ———– ———-

datastore1 751.314 926.500

PowerCLI C:> get-vmhost

Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz MemoryUsageGB MemoryTotalGB Version

—- ————— ———- —— ———– ———– ————- ————- ——-

172.232.1.129 Connected PoweredOn 2 137 5984 5.568 5.921 5.1.0

PowerCLI C:> New-VM -name SUSE-CLONE1 -VM SUSE-SRV1 -Datastore datastore1 -VMHost 172.232.1.129

Name PowerState Num CPUs MemoryGB

—- ———- ——– ——–

SUSE-CLONE1 PoweredOff 1 0.047

PowerCLI C:> get-vm

Name PowerState Num CPUs MemoryGB

—- ———- ——– ——–

VMware vCenter Se… PoweredOn 2 8.000

SUSE-SRV1 PoweredOn 1 0.047

SUSE-CLONE1 PoweredOff 1 0.047

PowerCLI C:>

 

Delete a VM along with its Disk files.

 

PowerCLI C:> Remove-VM SUSE-CLONE1 -DeletePermanently -Confirm:$false

PowerCLI C:>

 

 

 

 

 

Resources:

VMware vSphere PowerCLI Overview

Leave a Reply

Your email address will not be published. Required fields are marked *