For some business reasons we need to change some VMDK Hard Disks from Independent-Persistent to Dependent (and some of them from Dependent to Independent-Persistent) however powering off the VMs is not a viable option. It seems like it is no longer possible to use a PowerCLI command such as:
Get-VM VMName | % {Get-HardDisk -VM $_ | % {Set-HardDisk -HardDisk $_ -Persistence "Persistent" -Confirm:$false} }
So we’re looking for a way to somehow “schedule” the configuration, similar to the concept of the “Compatibility->Schedule VM Compatibility Upgrade” option. And since most of these VMs are Windows-Based, they have to reboot for Updates at least once a Month, so this would be a good opportunity to get this setting applied.
Any assistance to either issue a command that could apply upon system restart to adjust the persistence as needed, or a recommendation to leverage the Windows Update restart timing to somehow create a scheduled task to gracefully shutdown the VM, perform the persistence adjustment and then power the VM on, maybe right before applying the Windows Updates? (I believe a restart would not be sufficient for the persistence adjustment, but I may be wrong)
I wish there was a way to be able to control this setting even if it would not be fully active until next restart. Are there any ideas? Anyone had to deal with this challenge and found a way to perform this activity to several machines that need to be powered-on 99.99% of the time? Suggestions will be really appreciated!
There is indeed no way to do this for a powered on VM (anymore).
Your best option would be to use a script that does the following:
All these steps can be scripted.
You could even schedule this script (via for example the Windows Task Scheduler) on a management station that has the required access.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
There is indeed no way to do this for a powered on VM (anymore).
Your best option would be to use a script that does the following:
All these steps can be scripted.
You could even schedule this script (via for example the Windows Task Scheduler) on a management station that has the required access.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much for your response LucD!
So, on the power-off, fix and power-on process, I have tried running this as a PS1 script but evidently it is not correctly written as it is not doing what is needed:
Get-VM VMName | Stop-VM -Confirm:$false
Get-VM VMName | % {Get-HardDisk -VM $_ | % {Set-HardDisk -HardDisk $_ -Persistence "Persistent" -Confirm:$false} }
Get-VM VMName | Start-VM -Confirm:$false
As I test the above, I can see that even though it is NOT running asynchronously, the first line to power-off completes very quickly, and while I can see that the VM is on its way down, the persistence setting gets applied successfully and then even though the machine just started to gracefully shut down, the command to power-on executes and the VM comes up immediately. And as I login to it, the VM informs that it was unexpectedly restarted.
Long story short, is there a way for me to write the script above in a better way so that the power-off command actually waits for the VM to gracefully shutdown before running the next line?
You can use a loop after the Stop-VM.
Something like this for example
do{
sleep 2
$vm = Get-Vm -Name $vm.Name
}
until($vm.PowerState -eq 'PoweredOff')
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LucD! That definitely helped a lot!
I ended up replacing the STOP-VM command with a ShutDown-VMGuest command, and using your suggestions, I came up with this script:
$VM = Get-VM VMName
ShutDown-VMGuest -VM $VM -Confirm:$False
Do{Sleep 5
$VM = Get-VM -Name $VM.Name}Until($VM.PowerState -eq 'PoweredOff')
% {Get-HardDisk -VM $VM | % {Set-HardDisk -HardDisk $_ -Persistence "Persistent" -Confirm:$False} }
Start-VM $VM -Confirm:$False
I tested it and it seems to work exactly the way we need it. Of course, if you have any comments or suggestions to make it even better, that’ll be great.
Otherwise, I really appreciate all the time and help you provided LucD! It was really valuable for me! :smileycheck: