Can someone help me write a script on how to set the memory reservation and limit for a list of VMs from a text file. My lab is down right so I can't test my script to see if it works but i desperately need to make the change in production. I would be glad if anyone can tell me if they see anything wrong with it or any advice or input. Thanks
Get-Content -Path C:\tmp\VMList.txt | ForEach-Object {
Get-VM -Name $_ | `
Get-VMResourceConfiguration | `
Set-VMResourceConfiguration -MemReservationMB 1024 -MemLimitMB 2048
}
VMList.txt
vm1
vm2
vm3
vm4
vm5
That looks good.
Add the WhatIf switch on the Set-VMResourceConfiguration cmdlet, that way you can see what will happen.
And btw, you don't have to add the back-tick when the line ends with the pipeline symbol.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If i may..
Your script looks good, and to push it a little you could do something like this
Get-Content C:\tmp\VMList.txt | Get-VM -Name $_ | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemReservationMB 1024 -MemLimitMB 2048
The Foreach isnt needed because Get-Content send an array object to the pipeline (like foreach )
If i remove the foreach i get an error message (See below). My script seems to work fine. I tried it on 5 vms this morning.
Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or
empty. Supply an argument that is not null or empty and then try the command ag
ain.
At C:\Users\baptisec_pc\Desktop\VDI\PowerCLI Scripts\VMMemory2.ps1:2 char:13
+ Get-VM -Name <<<< $_ |
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValid
ationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
ation.ViCore.Cmdlets.Commands.GetVM
That is a known issue with passing strings through the pipeline and OBN.
Try like this
Get-VM -Name (Get-Content C:\tmp\VMList.txt) | Get-VMResourceConfiguration | Set-VMResourceConfiguration -MemReservationMB 1024 -MemLimitMB 2048
The difference is that you now pass an array of names on the Name parameter of the Get-VM
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It works. Unlike my script, it randomly select a name from the list.Still works. Thanks.
PowerCLI C:\Users\baptisec_pc\Desktop\VDI\PowerCLI Scripts> .\VMMemory3.ps1
VM NumCpuShares CpuSharesLevel NumMemShares MemSharesLevel
-- ------------ -------------- ------------ ----------
W7-VPC_E189 1000 Normal 30720 Normal
W7-VPC_E187 1000 Normal 30720 Normal
W7-VPC_E185 1000 Normal 30720 Normal
W7-VPC_E188 1000 Normal 30720 Normal
W7-VPC_E186 1000 Normal 30720 Normal