Hello!
Guys, please kindly help me with my task. I need to reconfigure virtual servers CPU and RAM. That is why I need to the follwoing steps by PowerCLI
1) Shut server down
2) Change CPU and RAM
3) Power on the server
I guess i will have a csv file with 3 fileds(servername, cpu value, ram value)
Can you please give me initail push, example or tutorail to get this task done.
Another questions how list of servers will be processed? Will each server will be processed one by one or it is able to implement the steps above at the same time for each server in the list?What can you advise in this context?
Thank you in advance!
Try something like this
Import-Csv c:\vms.csv -UseCulture | %{ $vm = Get-VM -Name $_.VMName
Shutdown-VMGuest -VM $vm -Confirm:$false
while($vm.PowerState -ne "PoweredOff"){ sleep 5
$vm = Get-VM -Name $_.VMName
} Set-VM -VM $vm -MemoryGB $_.MemoryGB -NumCpu $_.NumCpu
Start-VM -VM $vm -Confirm:$false
}
The CSV looks like this
"VMName","MemoryGB","NumCpu"
"VM1","8","2"
"VM2","4","1"
This script will do the stop-changes-start sequence 1 VM at a time.
You can do this in parallel, but that will require the script to keep track of each of the background tasks before starting the next step
Note: the script assumes that the VMware Tools are installed on the VMs.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
Import-Csv c:\vms.csv -UseCulture | %{ $vm = Get-VM -Name $_.VMName
Shutdown-VMGuest -VM $vm -Confirm:$false
while($vm.PowerState -ne "PoweredOff"){ sleep 5
$vm = Get-VM -Name $_.VMName
} Set-VM -VM $vm -MemoryGB $_.MemoryGB -NumCpu $_.NumCpu
Start-VM -VM $vm -Confirm:$false
}
The CSV looks like this
"VMName","MemoryGB","NumCpu"
"VM1","8","2"
"VM2","4","1"
This script will do the stop-changes-start sequence 1 VM at a time.
You can do this in parallel, but that will require the script to keep track of each of the background tasks before starting the next step
Note: the script assumes that the VMware Tools are installed on the VMs.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD!
Thanks for your quck responce. I have a pair of questions regarding your script.
Can i specify Datacenter and Cluster in DataCenter as parameter to look for VM?
And waht does mean "%" in first string?
Yes, you can use a Datacenter as a oarameter, but does this mean you will not get the VM names from a CSV file anymore ?
The '%' is an alias for the ForEach-Object cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oh not really! csv file will be used as source. Thanks for you quick support!!!!!!!!
P.S. Myabe you could give a hint how to check if VMTool needs to be update for virtual machine.
To check if the VMware Tools need to be upgraded, you can do
Get-VM | where {$_.ToolsVersionStatus -ne "guestToolsCurrent"} |
Select Name,ToolsVersionStatus
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD!
I need your help again:smileyconfused:
I am checking vmtools current version and if it needs to be updated i will execute Update-Tools -NoReboot. But how can i prevent executing following command to shutdown VM till updating is done? Is it really nice to use sleep in this case or there is some cmdlet to check current operation status?
....
Thanks u again!
You can check in a loop till the toolstatus changes
Something like this
if($vm.ToolsVersionStatus -ne "guestToolsCurrent") { $vm|Update-Tools -NoReboot
} while ($vm.ToolsVersionStatus -ne "guestToolsCurrent"){ sleep 5
$vm = Get-VM $vm.Name
} Shutdown-VMGuest -VM $vm -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference