PowerCLI One Liners

I am assuming that you are connected to vCenter server(s) in CLI to execute these commands. If not already you can do so with the below cmdlet

Connect-VIServer ‘VCName’

 

To query VM’s hardware version and VMware tools version:

Get-VM ‘VMName’ | Select Name,Version,@{N=”ToolsVersion”;E={$_.Extensiondata.Config.Tools.ToolsVersion}}

Get-VM (Get-Content .\Input.txt) | select Name,Version,@{N=”ToolsVersion”;E={$_ | Get-VMGuest | select -ExpandProperty ToolsVersion}} | Export-Csv -NoTypeInformation -Path .\Result.csv

 

To query network adapter type on list of VMs:

$list = Get-Content .\VMs.txt; foreach($line in $list) { Get-VM $line | Get-NetworkAdapter | Select @{N=‘VM’;E={$line}},Name,Type | Export-Csv  -path .\Report.csv -NoTypeInformation -Append }

 

To update network adapter type on a VM(experimental):

The below cmdlet changes network adapter from ‘E1000’ to ‘VMXNET3’. Though the changes reflect in VM level I see corresponding drivers are not getting updated in OS. Please comment if it works for you.

Get-VM VMName | Get-NetworkAdapter | Where { $_.Type -eq “E1000”} | Set-NetworkAdapter -Type “vmxnet3” -confirm:$false

 

To add portgroup:

The below cmdlet can be used to create a VM portgroup on all the hosts in a standard vSwitch cluster

Get-Cluster ‘ClusterName’ | Get-VMHost | foreach { New-VirtualPortGroup -Name ‘PGName’ -VirtualSwitch (Get-VirtualSwitch -Name vSwitch0 -VMHost $_) -VLanId 1000 -Confirm:$false }

 

To query Independent disks on VMs:

The below cmdlet can retrieve persistence mode of VM harddisks. Useful particularly when you want to identify Independent mode harddisks which cannot be backed up by snapshot based backup applications.

Get-Cluster ‘ClusterName’ | Get-VM | Get-HardDisk | Select Parent,Name,Persistence | Export-Csv -Path .\Report.csv -NoTypeInformation

 

To query the available drives and disk usage on VMs:

(Get-VM vmname).Guest.Disks |  Select Path,CapacityGB,FreespaceGB

 

The below line can grab the details for all the VMs in a vCenter

Get-VM | where {$_.PowerState -eq ‘PoweredOn’} | foreach{$vmname = $_.Name; $_.Guest.Disks | Select @{N=‘VMName’;E={$vmname}},Path,CapacityGB,FreespaceGB | Export-Csv -NoTypeInformation -Path .\Result.csv -Append}

 

To query if any memory limit configured for the VM:

Note: -1 in MemLimitGB column indicate no limit set for the VM (‘Unlimited’ checkbox ticked under Resource allocation section)

Get-VM ‘VMName’ | Select Name,MemoryGB,@{N=‘MemLimitGB’;E=$_ | Get-VMResourceConfiguration | select -ExpandProperty MemLimitGB }}

 

 

Who accessed VM console

The below script captures the ‘Remote Console Connected’ event entries for all the VMs in a given vCener. It can be useful in case if someone wants to audit the information about who accessed the servers.


$vc = Read-Host "Enter vCenter name you want to connect to"
Connect-VIServer $vc
$data = @()
$vms = Get-VM
foreach($vm in $vms)
{
Write-Host "Checking Events for VM -" $vm
$t = Get-VIEvent -Entity $vm | where { $_.FullFormattedMessage -match "Remote Console Connected" } | select UserName,CreatedTime,FullFormattedMessage
$row = @()
$row = " " |Select VMName,AccessedUser,AccessTime,Description
$row.VMName = $vm.Name
$row.AccessedUser = $t.UserName
$row.AccessTime = $t.CreatedTime
$row.Description = $t.FullFormattedMessage
if($t -ne $null ) { $data += $row }
}
$data | Export-Csv -Path .\Result.csv -NoTypeInformation
Disconnect-VIServer $vc -Confirm:$false

If your vCenter has many VMs the script will take a while to complete. You can get the output in csv file.

Thanks for reading!

Multiple VMs performance report in single excel

Our native vSphere client does not allow us to collect performance report of multiple entities in one shot. I wrote the below script which can grab performance report of multiple VMs in a single csv file. We can use the data in csv, draw charts, get a clear view and comparison of resource utilization / performance of multiple VMs.


##################################################################
# Script for collecting performance report of multiple VMs #
##################################################################
$vCenter = Read-Host "Enter the vCenter name wherein the target cluster resides"
$list = Read-Host "Enter path to text file that contains list of VMs to which performance report need to be grabbed"
$sdate = Get-Date (Read-Host "Enter start date of the report(mm/dd/yyyy)")
$fdate = Get-Date (Read-Host "Enter finish date of the report(mm/dd/yyyy)")
$nsamples = Read-Host "How many samples you want to be retrieved in specified interval"
$VMs = Get-Content $list
$stats = @()
Connect-VIServer $vCenter
foreach($VM in $VMs)
{
Write-Host "Collecting data for" $VM "…"
$stats += Get-Stat -Entity $VM -Common -MaxSamples $nsamples -Start $sdate -Finish $fdate
}
$stats | Export-Csv -Path .\PerfResult.csv -Force -NoTypeInformation
Invoke-Item .\PerfResult.csv
Disconnect-VIServer $vCenter -Confirm:$false

You need to supply the data in format according to date setting in local system in which script is being executed (mm/dd/yyyy in my case).

The –Common switch in Get-Stat commandlet retrieves the below metrics by default.
cpu.usage.average
cpu.usagemhz.average
mem.usage.average
disk.usage.average
net.usage.average
sys.uptime.latest
However more mertics can be retrieved using -Stat switch by supplying input in format – “counter group”.”counter name”.”rollup type”, Eg : cpu.usage.min.

The below is the nice pivot chart I got, showing the average CPU usage of two VMs over time (stacked graph).

VM Performance (Stacked graph)

Thanks for reading !