Posts tagged powercli
Resolving VM Memory Limits with PowerCLI
Dec 14th
During a health check of a pre-existing environment using the vCheck script, I found a lot of VMs with Memory Limits configured. After further investigation, these limits did not appear to be on purpose, but perhaps from a previous template or mis-configuration.
Since there were probably about 50 or so, I wanted to attack this in an automated manner instead of manually editing the settings of each VM, comparing configured memory with the limit, and so on. I found a lot of good resources for clearing them in a sweeping manner, but I wanted an approach that would allow me to first get a report of what the configured memory is and what the configured limit is. Unable to find these, I spent a few minutes concocting the following set of commands:
Get-VM | Foreach-Object -Process { Tee-Object -InputObject $_ -Variable Temp | Get-VMResourceConfiguration | where {$_.MemLimitMB -ne ‘-1′} } | Select VM,@{N=”MemoryMB”;E={$Temp.MemoryMB}},MemLimitMB
This returns in a format as follows:
So, based on the output there may be some VMs with no real reason to have a memory limit since it is equal to their configured memory anyway, but there are also some that have memory limits much lower than their configured memory. If I just cleared all memory limits, I’d have several machines with memory possibly over-allocated. Instead of clearing them all, I found the ones that had these values equal via the following command:
Get-VM | Foreach-Object -Process { Tee-Object -InputObject $_ -Variable Temp | Get-VMResourceConfiguration | where {$_.MemLimitMB -eq $Temp.MemoryMB} } | Select VM,@{N=”MemoryMB”;E={$Temp.MemoryMB}},MemLimitMB
Then I went ahead and cleared those via:
Get-VM | Foreach-Object -Process { Tee-Object -InputObject $_ -Variable Temp | Get-VMResourceConfiguration | where {$_.MemLimitMB -eq $Temp.MemoryMB} } | Set-VMResourceConfiguration -MemLimitMB $null
To reiterate, the aforementioned clears out any memory limits where the limit is the same size as the configured memory. This will spawn sequential tasks within vCenter that, from my runs, took about ~6 seconds each:
For the list of virtual machines with limits set different from their configured memory, those will be addressed manually with re-sizing if appropriate.
Documenting Environment Settings with PowerCLI
Aug 12th
Below are some PowerCLI snippets I have used to quickly gather information on an environment, as well as to help document settings. The following were grabbed from PowerCLI gurus such as LucD and Alan Renouf, and I have tried to cite the author’s work with each snippet (Note: I was unable to find where I grabbed some of these, but please shoot me an email if I accidentally didn’t cite the source).
These all exports into CSV format to allow easy merging into a document. I tend to include the code to grab the information within the overview document to allow the data to be easily updated on a periodic basis by anyone with PowerCLI installed, as well.
Cluster and Host Hardware Overview
Get-VMHost | Select Parent,Name,Manufacturer,Mode,Version,MemoryTotalMB,Model,NumCpu | Export-Csv “PathHere”
Datastore Capacity and Usage (Credit: LucD.info)
$DS = @()
Get-Cluster | ForEach-Object {
$Cluster = $_
$Cluster | Get-VMHost | ForEach-Object {
$VMHost = $_
$VMHost | Get-DataStore | Where-Object { $_.Name -notlike "local*"} | ForEach-Object {
$out = "" | Select-Object Cluster, DSName, FreespaceGB, CapacityGB, PercentFree
$out.Cluster = $Cluster.Name
$out.DSName = $_.Name
$out.FreespaceGB = $($_.FreespaceMB / 1024).tostring("F02")
$out.CapacityGB = $($_.CapacityMB / 1024).tostring("F02")
$out.PercentFree = (($_.FreespaceMB) / ($_.CapacityMB) * 100).tostring("F02")
$DS += $out
}
}
}
$DS | Sort-Object Cluster, DSName –Unique | Export-Csv “PathHere”
List of iSCSI Targets by Host (Credit: LucD.info)
Get-VMHost | Get-View | %{
$esx = $_
$esx.Config.StorageDevice.HostBusAdapter | where {$_.GetType().Name -eq "HostInternetScsiHba"} | %{
$hba = $_
$_.ConfiguredSendTarget | `
Select @{N="ESX Name";E={$esx.Name}},
@{N="HBA Device";E={$hba.Device}},
@{N="IScsi Name";E={$hba.IScsiName}},
@{N="IScsi Target";E={$_.Address}}
}
} | Export-Csv "PathHere" -NoTypeInformation –UseCulture
Service Console Details by Host (Credit: Virtu-al.net)
Get-VMHost | Get-VMHostNetwork | Select Hostname, ConsoleGateway, DNSAddress -ExpandProperty ConsoleNic | Select Hostname, PortGroupName, IP, SubnetMask, ConsoleGateway, Devicename | Export-Csv “PathHere”
VMKernel Details by Host
Get-VMHost | Get-VMHostNetwork | Select Hostname, VMkernelGateway -ExpandProperty VirtualNic | Select Hostname, PortGroupName, IP, SubnetMask, VMkernelGateway, Devicename | Export-Csv "PathHere"
Port Group Details by Host
Get-VMHost | ForEach-Object -Process {
Tee-Object -InputObject $_ -Variable Temp | Get-VirtualPortGroup |
Select @{N="VMHost";E={$Temp.Name}},Name,VirtualSwitch,VirtualSwitchName,VLanId
} | Export-Csv "PathHere”
vSwitch Details by Host (Credit: Virtu-al.net)
$NetworkInfo = @()
Foreach ($VMHost in (Get-View -ViewType HostSystem | Where {$_.Runtime.ConnectionState -ne "disconnected"})){
Write $VMHost.Name
$NetworkSystem = Get-View $VMHost.ConfigManager.NetworkSystem
Foreach ($PG in $NetworkSystem.NetworkInfo.PortGroup){
$Details = "" | Select VMHost, vSwitch, PortGroup, ActiveNics, StandbyNics
$Details.VMHost = $VMHost.Name
$Details.Portgroup = $PG.Spec.Name
If ((($PG.ComputedPolicy.NicTeaming.NicOrder.ActiveNic | Select -ExpandProperty $ActiveNic).Length) -gt 1){
$Details.ActiveNics = [string]::join(';',($PG.ComputedPolicy.NicTeaming.NicOrder.ActiveNic | Select -ExpandProperty $ActiveNic))
}
Else {
$Details.ActiveNics = ($PG.ComputedPolicy.NicTeaming.NicOrder.ActiveNic | Select -ExpandProperty $ActiveNic)
}
If ((($PG.ComputedPolicy.NicTeaming.NicOrder.StandbyNic | Select -ExpandProperty $StandbyNic).Length) -gt 1){
$Details.StandbyNics = [string]::join(';',($PG.ComputedPolicy.NicTeaming.NicOrder.StandbyNic | Select -ExpandProperty $StandbyNic))
}
Else{
$Details.StandbyNics = ($PG.ComputedPolicy.NicTeaming.NicOrder.StandbyNic | Select -ExpandProperty $StandbyNic)
}
Foreach ($VS in $NetworkSystem.NetworkInfo.vSwitch){
If ($VS.Name -eq $PG.Spec.vSwitchName){
$Details.vSwitch = $VS.Name
}
}
$NetworkInfo += $Details
}
}
$NetworkInfo | Sort VMHost, VSwitch, PortGroup | Export-Csv "PathHere" –NoTypeInformation
Another great tool that should be in every Admin’s tool-belt is RVTools, which leverages PowerCLI to generate a great deal of information. All of the data generated through RVTools can also be exported to CSV and merged into the documentation.
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
PCoIP VRAM Fix
Aug 30th
One of the biggest annoyances I’ve ran into when deploying VMware View is the finickiness of PCoIP. The problem is that if the video settings within your VM are not just right, then you will run into issues like:
- You cannot re-size the screen
- Resizing only works down instead of up.
- Resizing responds very slowly or fails after several resize attempts.
- You cannot switch between full-screen and windowed.
Obviously, these are rather deal-breaker bugs. To say the least, VMware’s response and fix to the issue are disappointing. Their “fix” involves jumping through a lot of hoops that make the process way more tedious than it should be. The KB article for this fix is 1018158.
It doesn’t appear that the vSphere client saves changes to the Video Settings fully to the .vmx file, but fortunately ermac318 over at VM Junkie has created a PowerCLI function to fix this issue. You can grab the script from his site or from a local mirror here. To run it, do the following:
> Connect-VIServer -server vCenterServer.tld
> . .\set-vramsize.ps1
> $vm = Get-VM -name VirtualMachineToModify
> Set-VRamSize($vm)
This should modify the .vmx file for the VM. You can confirm by downloading the .vmx file through vCenter and seeing if the file contains the expected modifications.

