Skip to content

Instantly share code, notes, and snippets.

@mattvanstone
Created February 2, 2016 17:17
Show Gist options
  • Save mattvanstone/fb4e626f9be0c01e16e0 to your computer and use it in GitHub Desktop.
Save mattvanstone/fb4e626f9be0c01e16e0 to your computer and use it in GitHub Desktop.
Get-VMWithMemLimit/Remove-VMMemLimit PowerCLI Functions - Quickly find VMs with memory limits and remove the limit
<#
.SYNOPSIS
Function gets VMs that have memory limits configured.
.DESCRIPTION
Takes a list of vms input from Get-VM and checks each one for a configured memory limit
.PARAMETER vms
The vm(s) to check for memory limits
.EXAMPLE
Get-VM | Get-VMWithMemLimit
.EXAMPLE
$vms = Get-VM
Get-VMWithMemLimit -vms $vms
#>
Function Get-VMWithMemLimit {
[CmdletBinding()] Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]$vms
)
Begin {
$list = @()
}
Process {
$vms | % {
$res = $_ | Get-VMResourceConfiguration | Select MemLimitMB
if ($res.MemLimitMB -ne "-1") {
$item = "" | Select Name,MemoryMB,MemLimitMB
$item.Name = $_.Name
$item.MemoryMB = $_.MemoryMB
$item.MemLimitMB = $res.MemLimitMB
$list += $item
}
}
}
End {
return $list
}
}
<#
.SYNOPSIS
Removes the memory limit configured on a VM.
.DESCRIPTION
Takes a list of vms as input from Get-VM and removes the memory limit from each one
.PARAMETER vms
List of VMs to remove limit from
.EXAMPLE
Get-VM | Remove-VMMemLimit
.EXAMPLE
Get-VMWithMemLimit | Remove-VMMemLimit
#>
Function Remove-VMMemLimit {
[CmdletBinding()] Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]$vm
)
Begin {
}
Process {
$vm | % {
$spec = new-object VMware.Vim.VirtualMachineConfigSpec
$spec.MemoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo
$spec.MemoryAllocation.Limit = -1
$vm = get-view -ViewType VirtualMachine -Filter @{"Name"=$_.Name}
$vm.ReconfigVM_Task($spec)
}
}
}
@mattvanstone
Copy link
Author

A while ago I noticed that someone had set a memory limit on one of the VM templates in my environment. This resulted in a lot of VMs with memory limits. I wrote these functions so that I can quickly check the environment to find memory limits and remove them if they shouldn't be there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment