Skip to content

Instantly share code, notes, and snippets.

@chaderoth
Last active October 11, 2018 22:08
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/bb162f90a415cd43d48f4926f716ad9a to your computer and use it in GitHub Desktop.
Save chaderoth/bb162f90a415cd43d48f4926f716ad9a to your computer and use it in GitHub Desktop.
############################################
# Get User Credentials To Pass To Guest OS #
############################################
$Creds = Get-Credential
######################################################################
# Get All CentOS Linux VMs With A Configured ISO Path #
# Use Invoke-VMScript To Eject The Mounted Media Inside The Guest OS #
# Disconnect The ISO From The VM #
######################################################################
Foreach ($VM in (Get-VM | Where-Object {$_.Guest.OSFullName -like "*CentOS*"} | Get-CDDrive | Where-Object {$_.IsoPath -ne $null} | Select-Object *)){
Write-Host "Ejecting CD-ROM In Guest For "$VM.Parent.Name""
Invoke-VMScript -ScriptText "eject" -ScriptType Bash -VM $VM.Parent.Name -GuestCredential $Creds
Write-Host "Disconnecting The ISO From "$VM.Parent.Name""
Get-CDDrive -VM $VM.Parent.Name | Set-CDDrive -NoMedia -Confirm:$false > $null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment