A quick blog on some one liners managing the vmware estate an admin must have. 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
## Outputs the VM Name, the Disk Name, VMDK Datastore and FileName and Capacity in GB to C:\Temp\VMVHDs.csv for all VMs associated with the PowerCLI-connected vSphere server
$report = @(); $vms = get-vm; foreach($vm in $vms) {$vhds = Get-HardDisk $vm ; foreach ($vhd in $vhds){ $row = "" | select VMName, Name, FileName, CapacityGB; $row.VMName = $vhd.Parent; $row.Name = $vhd.Name; $row.FileName = $vhd.FileName; $row.CapacityGB = $vhd.CapacityGB; $report += $row;} } $report | export-csv C:\Temp\VMVHDs.csv -NoTypeInformation
## Get Running VMs without VMware Tools Installed
Get-VM | where {$_.powerstate -eq "PoweredOff"} | Sort-Object | get-harddisk | Select parent, capacityGB, Filename | ft -a
## 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
## Count How Many VMS running on the hosts
Get-VMHost | Select @{N="Cluster";E={Get-Cluster -VMHost $_}}, Name, @{N="NumVM";E={($_ | Get-VM).Count}} | Sort Cluster, Name
## List All VM’s disk space
ForEach ($VM in Get-VM ){($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(MB)";E={[math]::Round($_.Capacity/ 1MB)}}, @{N="Free Space(MB)";E={[math]::Round($_.FreeSpace / 1MB)}}, @{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}})}
## 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
## Outputs the VM Name and .vmx file path to C:\Temp\VMxList.csv for all VMs associated with the PowerCLI-connected vSphere server
$report = @(); $vms = Get-VM; foreach ($vm in $vms) {$row = "" | select VMName, VMXPath; $row.VMName = $vm.Name; $row.VMXPath = $vm.ExtensionData.Config.Files.VmPathName; $report += $row;} $report | export-csv C:\Temp\VMxList.csv -NoTypeInformation
## Delete all Snapshots with Certain Name
Get-VM | Sort Name | Get-Snapshot | Where { $_.Name.Contains(“Consolidate”) } | Remove-Snapshot
## Outputs the VMName, VM Configuration File path, VHDCount, VHD Location, VHDSize, MaxVHDSize, Cluster for all VMs managed by the SCVMM server connected by PowerShell
$report = @(); $vms = get-vm; foreach ($vm in $vms){foreach ($vhd in $vm.VirtualHardDisks){$maxvhdsize = [math]::Round($vhd.MaximumSize/1024/1024/1024,1); $vhdsize = [math]::Round($vhd.Size/1024/1024/1024,1); $row = "" | select VMName, VMCPath, VHDCount, VHDLocation, VHDSize, MaxVHDSize, VMCluster; $row.VMName = $vm.Name; $row.VMCPath = $vm.VMCPath; $row.VHDCount = $vm.VirtualHardDisks.Count; $row.VHDLocation = $vhd.Location; $row.VHDSize = $vhdSize; $row.MaxVHDSIze = $MaxVHDSize; $row.VMCluster = $vm.VMHost.HostCluster.Name; $report += $row;};};$report | export-csv C:\Temp\Hyper-V-VMs.csv -NoTypeInformation
## Get Host Hardware information
Get-VMHost |Sort Name |Get-View |Select Name, @{N=“Type“;E={$_.Hardware.SystemInfo.Vendor+ “ “ + $_.Hardware.SystemInfo.Model}},@{N=“CPU“;E={“PROC:“ + $_.Hardware.CpuInfo.NumCpuPackages + “ CORES:“ + $_.Hardware.CpuInfo.NumCpuCores + “ MHZ: “ + [math]::round($_.Hardware.CpuInfo.Hz / 1000000, 0)}},@{N=“MEM“;E={“” + [math]::round($_.Hardware.MemorySize / 1GB, 0) + “ GB“}}
## Query ESXi Host Management Networks
Get-VMHost | Get-VMHostNetwork | Select Hostname, VMKernelGateway -ExpandProperty VirtualNic | Where {$_.ManagementTrafficEnabled} | Select Hostname, PortGroupName, IP, SubnetMask
## Query ESXi Host vMotion Networks
Get-VMHost | Get-VMHostNetwork | Select Hostname, VMKernelGateway -ExpandProperty VirtualNic | Where {$_.vMotionEnabled} | Select Hostname, PortGroupName, IP, SubnetMask
## Get ESXi Host BIOS version and date(s)
Get-View -ViewType HostSystem | Sort Name | Select Name,@{N="BIOS version";E={$_.Hardware.BiosInfo.BiosVersion}}, @{N="BIOS date";E={$_.Hardware.BiosInfo.releaseDate}}