VMware Cloud Community
simonadams
Contributor
Contributor
Jump to solution

Gracefully shut down VMs and Disconnect FDD/CD

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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

simonadams
Contributor
Contributor
Jump to solution

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 ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
simonadams
Contributor
Contributor
Jump to solution

Very helpful as always ... many thanks Luc

Reply
0 Kudos