Posts tagged powershell
Nifty PowerCLI One-Liners
Oct 15th
The following are a few PowerCLI commands that I have found useful at one time or another. The VMware Communities section for PowerCLI is a great starting point for more information on setting up and using PowerCLI. If you have a question about how to do something, the discussion forum is a great resource; there are tons of incredibly intelligent and helpful individuals that will be more than happy to point you in the right direction.
Get Running VMs without VMware Tools Installed:
Get-View -ViewType “VirtualMachine” -Property Guest,name -filter @{“Guest.ToolsStatus”=”toolsNotInstalled”;”Guest.GuestState”=”running”} | Select Name
List all Snapshots:
Get-VM | Sort Name | Get-Snapshot | Select VM,Name,Description,Created
VMs Created Recently:
Get-VIEvent -maxsamples 10000 | Where {$_.Gettype().Name -eq “VmCreatedEvent”} | Select createdTime, UserName, FullFormattedMessage
VMs Removed Recently:
Get-VIEvent -maxsamples 10000 | Where {$_.Gettype().Name -eq “VmRemovedEvent”} | Select createdTime, UserName, FullFormattedMessage
VMs with more than 2 vCPUs:
Get-VM | Where {$_.NumCPU -gt 2} | Select Name, NumCPU
Check for invalid of inaccessible VMs:
Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template} | Where{$_.Runtime.ConnectionState -eq “invalid” -or $_.Runtime.ConnectionState -eq “inaccessible”} | Select Name
Get Errors in the last week:
Get-VIEvent -maxsamples 10000 -Type Error -Start $date.AddDays(-7) | Select createdTime, fullFormattedMessage
Get VMs with Memory Reservations:
Get-VM | Get-VMResourceConfiguration | Where {$_.MemReservationMB -ne 0} | Select VM,MemReservationMB
Get VMs with CPU Reservations:
Get-VM | Get-VMResourceConfiguration | Where {$_.CpuReservationMhz -ne 0} | Select VM,CpuReservationMhz
Delete all Snapshots with Certain Name:
Get-VM | Sort Name | Get-Snapshot | Where { $_.Name.Contains(“Consolidate”) } | Remove-Snapshot