I'm in need of a PowerCLI script to remove the CD/DVD drive from the existing virtual servers (around 100+).
Those 100 servers are in different vCenters and script would be able to pick up the servers list from a input file.
TIA
I'm afraid you may not be able to remove it from a vm that is powered on. Here is some updated error handling.
foreach ($vmname in $servernamelist){
Try {
Get-CDDrive -VM $vmname | Remove-CDDrive -Confirm:$false -ErrorAction Stop
Write-Output "removed CD-Drive from Server: $vmname"
}
Catch {
Write-Output "failed to remove CD-Drive from server: $vmname"
}
}
$servernamelist = Get-Content -Path "c:\someFilePath"
foreach ($vmname in $servernamelist){
Get-CDDrive -VM $vmname | Remove-CDDrive -Confirm:$false -ErrorAction SilentlyContinue
}
great, thank you. that helps. However, can the script generate an output like server name with status (CD-ROM removed etc)
abc.com CD/DVD1 removed
123.com CD/DVD1 unable to remove due to some error
$servernamelist = Get-Content -Path "c:\someFilePath"
foreach ($vmname in $servernamelist){
Try {
Get-CDDrive -VM $vmname | Remove-CDDrive -Confirm:$false
Write-Output “ removed CD-Drive from Server: $vmname”
}
Catch {
Write-Output “failed to remove CD-Drive from server: $vmname”
}
}
Sorry for the spacing, I’m on my phone.
thank you. but, it is not working as expected. Can you please re-look the script...
can you please tell me what error you are getting?
when the server is in powered ON state, it is throwing an error that - "The VM must be in the following state: PoweredOff"
and also displaying the message that "removed CD-Drive from server:" (this one displaying after the above error message)
I'm afraid you may not be able to remove it from a vm that is powered on. Here is some updated error handling.
foreach ($vmname in $servernamelist){
Try {
Get-CDDrive -VM $vmname | Remove-CDDrive -Confirm:$false -ErrorAction Stop
Write-Output "removed CD-Drive from Server: $vmname"
}
Catch {
Write-Output "failed to remove CD-Drive from server: $vmname"
}
}
thank you so much. it is working as expected.