Insanely simple and incredibly useful PowerCLI snipits

I find that I’m spending an increasing amount of time in PowerCLI, partly that’s because I am working with groups of hosts or vms. It’s not that I’m working with scripts all the time, just that if I need to do some action at scale, a foreach loop is going to save a bunch of time…

Here is my growing collection of simple one-liners…
#PowerCLI Rocks

General – working with groups of vms

List vms with Name
Get-VM | Where {$_.Name -like “Web*”}

Get by Blue Folder
$f = Get-Folder <“folder name”>
$folder = (Get-Folder $f | Get-View)
$foldervms = Get-View -SearchRoot $folder.MoRef -ViewType “VirtualMachine” | Select Name

Import vms from text file
$vms = Get-Content C:\vSpherePowerCLI\Input\vmlist.txt | foreach {Get-VM $_}

Move-VM -VM <vm> -DiskStorageFormat Thin -Datastore <datastore> -RunAsyn

Clone VM
New-VM -Name <new vm> -VM <original vm> -Datastore <destination datastore> -VMHost <destination host> -RunAsyn

Search for vms with mounted iso file
Get-VM | FT Name, @{Label=”ISO file”; Expression = { ($_ | Get-CDDrive).ISOPath }}



How many virtual wires?
$totalvwires = Get-VirtualPortGroup | ? {$_.Name -like “vxw-dvs*”}

How many ports?
$numports = Get-VirtualPortGroup | Select NumPorts
> $numports.count


Quick check of highest vm cpu ready stats
Get-Stat -entity $vm -IntervalSecs 300 -stat ‘cpu.ready.summation’ | sort-object value -descending | Select-Object -first 25 | format-table -auto

Quick check of highest stats
List possible stat types (availability on vcenter depends on configured Statistics Level)
Get-StatType -Entity $esx -Start (get-date) | Sort-Object -Unique
Get-Stat -entity $vm -IntervalSecs 300 -stat ‘<stat type>’ | sort-object value -descending | Select-Object -first 25 | format-table –auto


Check Guest OS disk assignment and usage
List OS view of disk assignment, disk name, capacity, and free space in GB and percentage
$VM = get-vm <vm name>
$vm.Extensiondata.Guest.Disk | Select @{N=”Name”;E={$vm.Name}},DiskPath, @{N=”Capacity(GB)”;E={[math]::Round($_.Capacity/ 1GB)}}, @{N=”Free Space(GB)”;E={[math]::Round($_.FreeSpace / 1GB)}}, @{N=”Free Space %”;E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}}

Get NFS Host disk assignment
C:\vSpherePowerCLI\Scripts> Get-Datastore | where {$_.type -eq “NFS”} | Select ParentFolder,Name,RemoteHost,RemotePath | ft

Get NFS Status for all hosts in cluster
$vmhosts = get-cluster <cluster> | get-vmhost
foreach ($vmhost in $vmhosts) {Get-Datastore | ? {$_.Type -eq “NFS”} | Select ParentFolder, RemoteHost, RemotePath, Type, Accessible| ft -a}

Get host lun information
Get-VmHostStorage -VMHost <hostname> | select -expandproperty scsilun | Select Islocal, CanonicalName, CapacityGB, Vendor, MultipathPolicy | ft -a
Get-VMhost -Name Host | Get-VMHostHBA -Type FibreChannel | Select VMHost, Device,@{N=”WWN”;E={“{0:X}” -f $_.PortWorldWideName}} | Sort VMhost, Device

Compare datastore visibility
(i.e. compare hosts attached to datastore)
$datastore1 = Get-datastore <datastore1> | Get-vmhost
$datastore2 = Get-datastore <datastore1> | Get-vmhost

Compare-Object $datastore1 $datastore

vCenter Migration and setup

Load vms/hosts from txt
$vmlist = Get-Content C:\PowerCLI\input\vmlist.txt | foreach { Get-VM $_ }

Stop multiple vms based imported from list
$vms = $vmlist
Get-VMGuest -VM $vms | Where-Object {$_.State -eq “Running”} | Shutdown-VMGuest -Confirm:$False

Remove vms from inventory (does not delete files)
foreach ($vm in $vms) {Remove-VM -VM $vm -DeleteFromDisk:$false -Confirm:$false -RunAsync}

Import folders
Import-Folders -FolderType “Blue” -DC “Barcelona” -Filename “C:\PowerCLI\Output\BlueBarcelona.csv”

Remove the default VM Network portgroup
foreach ($vmhost in $vmhosts) {Get-VirtualSwitch -VMHost $VMhost -Name vSwitch0 | Get-VirtualPortGroup -Name “VM Network” | Remove-VirtualPortGroup -Confirm:$false

{Get-VirtualSwitch -VMHost $VMhost -Name vSwitch0 | Get-VirtualPortGroup -Name “VM Network” | remove-VirtualPortGroup -Confirm:$false } -ErrorAction SilentlyContinue

Restart group of vms
Start-VM -VM $vms -Confirm:$false –RunAsync

List vm guest OS
foreach ($vm in $vms){ get-vm $vm | get-vmguest | Select VM, GuestId | ft -a}

List vm datastores
foreach ($vm in $vm){ Get-vm $vm | Select Name, @{N=”Datastore”;E={Get-Datastore -VM $_}}}


Working with Tags and SBPM

List Tags of a certain category
Get-Tag | Where { $_.Category -like “StorageAffinity”}

List tags assigned to datastore
Get-TagAssignment -Entity (Get-datastore) | Select Entity, Tag | Sort-Object Tag

Add Tags to mutiple vms
$vms = Get-folder | get-vm
$Tag = Get-Tag room-2-vplex
foreach ($vm in $vms) { New-TagAssignment -Tag $Tag -Entity $vm}
foreach ($vm in $vms) { Get-TagAssignment -Entity $vm | Select Entity, Tag}

Assign Storage Policy
Get-vm | Get-SpbmEntityConfiguration
Get-vm <vm> | Set-SpbmEntityConfiguration -StoragePolicy <storage policy>
Get-vm <vm> | Get-SpbmEntityConfiguration

Working with Groups of Hosts

List VMotion Networks
Get-VMHost | Get-VMHostNetwork | Select Hostname, VMkernelGateway -ExpandProperty VirtualNic | Where {$_.VMotionEnabled} | Select Hostname, PortGroupName, IP, SubnetMask, VMkernelGateway, Devicename | Sort-Object IP | ft -a

List Management Networks
Get-VMHost <host> | Get-VMHostNetwork | Select Hostname, VMkernelGateway –ExpandProperty VirtualNic | Where {$_.ManagementTrafficEnabled} | Select Hostname, PortGroupName, IP, SubnetMask, VMkernelGateway, Devicename | Sort-Object IP | ft –a

List DNS and Gateway
Get-VMHost | Get-VMHostNetwork | Select Hostname, VMkernelGateway, DnsAddress

Restart host
Restart-VMHost <host> -RunAsync –Confirm
foreach ($vmhost in $vmhosts) { Restart-VMHost $vmhost -RunAsync –Confirm}






Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.