All
We have a VDI environment where the connection broker agent sometimes fails to shut down images when the user logs off, and I want to run a weekly script late at night to gracefully power them all down. Additionally, sometimes people attach local FDD / CD and these cause problems with DRS, so I want to remove all of these at the same time.
I could use the following;
Get-Cluster "VDS - SDC01" | Get-VM UK1* | Shutdown-VMGuest –confirm:$false
Get-Cluster "VDS - SDC01" | Get-VM UK1* | Stop-VM -confirm:$false
Get-Cluster "VDS - SDC01" | Get-Vm | Where-Object {
(((Get-CDDrive -VM $_ | Where-Object { (($_.ConnectionState.Connected -eq $True) -or ($_.ConnectionState.StartConnected -eq $True))} ) -ne $Null) `
-or `
((Get-FloppyDrive -VM $_ | Where-Object { (($_.ConnectionState.Connected -eq $True) -or ($_.ConnectionState.StartConnected -eq $True))} ) -ne $Null))
} | %{
"VM: " + $_.Name + " (" + $_.PowerState + ")"
Get-CDDrive -VM $_ | ForEach-Object { Set-CDDRIVE -CD $_ -StartConnected $False -Connected $False -Confirm:$False | %{ " Changed " + $_.Name} }
Get-FloppyDrive -VM $_ | ForEach-Object { Set-FloppyDRIVE -Floppy $_ -StartConnected $False -Connected $False -Confirm:$False | %{ " Changed " + $_.Name} }
But the the disconnect FDD/CD statement is a bit clumsy. All I want to do is set the VM's so that when they power on again, there are going to be no CD or FDD set so that they will cause problems with DRS (connected locally or on host).
Looking around I have seen an option of using Set-CDDrive -CD $cd -NoMedia and Set-FloppyDrive -Floppy $floppy -NoMedia
As all the VM's should be powered down at this stage, could I just replace the last line with something like
Get-Cluster "VDS - SDC01" | Get-Vm | {
Set-CDDrive -CD $cd -NoMedia
Set-FloppyDrive -Floppy $floppy -NoMedia
}
?
Regards
Simon
The "%" is an alias for Foreach-Object. You can check with
Get-Alias -Name %
All the VMs will be passed, one by one, to the code block after the %.
Inside the code-block the current VM is accessible through the $_ variable.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I think you need to do
Get-Cluster "VDS - SDC01" | Get-Vm | %{
Get-CDDrive -VM $_ | Set-CDDrive -CD $cd -NoMedia
Get-FloppyDrive -VM $_ | Set-FloppyDrive -Floppy $floppy -NoMedia
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Many thanks Luc - I will give that a go.
I will get the hang of the syntax and what is a property of what one day!
Out of interest, what does the "%" used there represent ?
The "%" is an alias for Foreach-Object. You can check with
Get-Alias -Name %
All the VMs will be passed, one by one, to the code block after the %.
Inside the code-block the current VM is accessible through the $_ variable.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Very helpful as always ... many thanks Luc