This is the top five powercli commands every VMware SA should have or know. I would also highly recommend starting out in the vSphere Documentation Center. It’s not really what I would consider bedtime reading but you can find some excellent new cmdlets and see usage.
1. Get-VM
It’s a classic. It has been around for awhile and we have to start with this one. Good old Get-VM! It is a great place to start. It can produce a simple inventory of all VMs that are currently found within vCenter . Using the Get-VM cmdlet, and the standard PowerShell Select-Object cmdlet, you can retrieve the particular properties that are of interest to you and use Export-CSV to export that data into a CSV file for further data manipulation.
Get-VM | Select-Object Name,NumCPU,MemoryGB,PowerState,VmHost
Export-CSV VMs.csv -NoTypeInformation
You can also use Get-VM to find snapshots you might have forgotten about in your environment.
Get-VM | Get-Snapshot | format-list | out-file c:\snapshots.txt
2. Move-VM
If you’re looking to move all the VMs from one host to another for possible maintenance, you can use the Move-VM cmdlet. You can also use Maintenance Mode as well, but we’re focusing on the CLI here! Below we are taking VMs on esxi01 and moving them to esxi02.
Get-VMHost lab-esxi01.labl.local | Get-VM | Move-VM -Destination (Get-VMHost lab-esxi02.lab.local)
Maybe you’re only interested in live migrating a single VM? This command will take a VM called MyVM and move it to the esxi01 host.
Move-VM -VM 'MyVM' -Destination 'lab-esxi01.lab.local'
With the command below, you can also do a storage vMotion. MyVM is the VM name and MyDatastore is your destination datastore.
Move-VM -VM 'MyVM' -Datastore 'MyDatastore'
3. New-VM
If you’re looking to build a single VM, you can use New-VM using your information to create a single VM. This basically builds a blank VM, you’ll have to deploy the OS, etc. You can see below for other options to deploy multiple VMs and use templates and customization specifications instead.
New-VM –Name “MyVM” –VMHost esxi01 –ResourcePool Production –DiskGB 20 –DiskStorageFormat Thin –DataStore datastore1 –MemoryGB 3 –NetworkName “ProdNetwork”
This is one I have used a lot in testing environments where you are cloning machines all the time. What this does below is creates a new machine called NewVM8 from a clone of OriginalVM. It will deploy your VM to the esxi01 host, although the ResourcePool parameter accepts ResourcePool, Cluster, vApp, and standalone VMHost objects so you can use any of those.
$myResourcePool = Get-ResourcePool -Name esxi01
$mySpecification = Get-OSCustomizationSpec -Name WindowsSpec
New-VM -VM OriginalVM -Name NewVM -OSCustomizationSpec $mySpecification -ResourcePool $myResourcePool
If you’re not wanting to run a script for each VM, it is possible to create multiple VMs from a CSV. This is a relatively basic way to get the job done. For multiple VMs, create a CSV like this.
Then after you have the CSV, you can use the New-VM cmdlet below and deploy multiple VMs. For this I typically don’t go crazy as it taxes vCenter if you have a ton of VMs.
$vms = Import-CSV C:\Scripts\NewVMs.csv
foreach ($vm in $vms){
$Template = Get-Template $vm.template
$VMHost = Get-VMHost $vm.host
$Datastore = Get-Datastore $vm.datastore
$OSCustomization = Get-OSCustomizationSpec $vm.customization
New-VM -Name $vm.name -OSCustomizationSpec $OSCustomization `
-Template $Template -VMHost $VMHost -Datastore $Datastore -RunAsync
}
4. Invoke-VMScript
How many times have you logged into a VM to run a script inside the guest operating system, only to logout again a few minutes later? This Invoke-VMScript cmdlet will save you the hassle of doing that.
The script below will run a BAT script. In BAT scripts, to access environment variables, you must use the following syntax: %<environment variable>% (for example, %programfiles%).
The outer quotes ($script = ‘…’) are required because this is how you define a string variable in PowerShell. The inner double quotes are required because there are spaces in the path.
$script = '"%programfiles%\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" /report "%tmp%\inforeport"'
Invoke-VMScript -ScriptText $script -VM VM -GuestCredential $guestCredential -ScriptType Bat
5. Get-VMHost
A common issue in virtual environments is NTP. Hosts are out of sync, then the login issue creep up. Performance monitoring quits working, etc. It’s overlooked a lot. You can use Host Profiles if you’ve got Enterprise Plus licensing, but you can also use the command below to modify and change the NTP settings on a host.
Get-VMHost esx01 | Add-VMHostNtpServer -NtpServer ntpservername
# Another good reason to use Get-VMHost would be to easily find what host a VM is currently running on.
$MyVM = Get-VM -Name MyVM
Get-VMHost -VM $MyVM
Wrap up:
To wrap up, these are just a few of the awesome cmdlets you can use, but these 5 are ones that every single VMware administrator should know and commit to your brain. VMware is always adding new features but these are a few you should commit to memory. If you’re a fan of other ones post them in the comment section and let us know how you use them.
Where’s the link to the CSV file?
Dazza, which csv are you referring to? If VM creation script, then refer to previous post https://virtuallythatguy.co.uk/create-vms-using-powercli-virtuallythatguy/
csv content
Name Template Host Datastore Customization
Ron-Test01 Win2019 lab-esxi01 lab-datastore-01 lab-join-ad
Ron-Test02 Win2019 lab-esxi02 lab-datastore-02 lab-join-ad
Ron-Test03 Win2019 lab-esxi03 lab-datastore-03 lab-join-ad
Ron-Test04 Win2019 lab-esxi04 lab-datastore-04 lab-join-ad
Ron-Test05 Win2019 lab-esxi05 lab-datastore-05 lab-join-ad