Skip to content

Instantly share code, notes, and snippets.

@chaderoth
Last active October 15, 2018 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaderoth/5949ba40996db94fafecadb0d70ac49f to your computer and use it in GitHub Desktop.
Save chaderoth/5949ba40996db94fafecadb0d70ac49f to your computer and use it in GitHub Desktop.
###########################################
# Get All VMs With A Configureed ISO Path #
# And Store In The Variable $VMs #
###########################################
$VMs = Get-VM | Get-CDDrive | Where-Object {$_.IsoPath -ne $null} | Select-Object *
######################################
# Get All Windows VMs Stored In $VMs #
# And Store In The Variable $WinVMs #
######################################
$WinVMs = $VMs | Where-Object {$_.Parent.Guest.OSFullName -like "*Windows*"}
###########################################
# Get All CentOS Linux VMs Stored In $VMs #
# And Store In The Variable $CentVMs #
###########################################
$CentVMs = $VMs | Where-Object {$_.Parent.Guest.OSFullName -like "*CentOS*"}
########################################################
# If A Windows VM Is Returned, #
# For Every VM Returned Disconnect The ISO From The VM #
########################################################
If($WinVMs){
Foreach ($VM in $WinVMs){
Write-Host ""
Write-Host "Disconnecting The ISO From "$VM.Parent.Name"" -ForegroundColor Yellow
Get-CDDrive -VM $VM.Parent.Name | Set-CDDrive -NoMedia -Confirm:$false > $null
Write-Host ""
}
}
Else{
Write-Host ""
Write-Host "No Windows VMs Have Been Found With Connected ISOs!" -ForegroundColor Green
Write-Host ""
}
##########################################################################
# If A CentOS Linux VM Is Returned, #
# Prompt For Credentials With Privileges To Eject The Media In The Guest #
# And Store The Credentials In The $Creds Variable #
# Then For Every VM Returned Eject The Media In The Guest #
# And Disconnect The ISO From The VM #
##########################################################################
If($CentVMs){
$Creds = Get-Credential
Foreach ($VM in $CentVMs){
Write-Host ""
Write-Host "Ejecting Media In Guest For "$VM.Parent.Name"" -ForegroundColor Yellow
Invoke-VMScript -ScriptText "eject" -ScriptType Bash -VM $VM.Parent.Name -GuestCredential $Creds
Write-Host ""
Write-Host "Disconnecting The ISO From "$VM.Parent.Name"" -ForegroundColor Yellow
Get-CDDrive -VM $VM.Parent.Name | Set-CDDrive -NoMedia -Confirm:$false > $null
Write-Host ""
}
}
Else{
Write-Host "No CentOS Linux VMs Have Been Found With Connected ISOs!" -ForegroundColor Green
Write-Host ""
}
Write-Host ""
Write-Host "Finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment