VMware Cloud Community
Mallik7
Enthusiast
Enthusiast
Jump to solution

In need of a PowerCLI script

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

1 Solution

Accepted Solutions
jpsider
Expert
Expert
Jump to solution

I'm afraid you may not be able to remove it from a vm that is powered on. Here is some updated error handling.

$servernamelist = Get-Content -Path "C:\temp\servers.txt"


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"

    } 

}

View solution in original post

9 Replies
jpsider
Expert
Expert
Jump to solution

$servernamelist = Get-Content -Path "c:\someFilePath"

foreach ($vmname in $servernamelist){   

    Get-CDDrive -VM $vmname | Remove-CDDrive -Confirm:$false -ErrorAction SilentlyContinue

}

Reply
0 Kudos
Mallik7
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
jpsider
Expert
Expert
Jump to solution

$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”

     }  

}

Reply
0 Kudos
jpsider
Expert
Expert
Jump to solution

Sorry for the spacing, I’m on my phone.  

Reply
0 Kudos
Mallik7
Enthusiast
Enthusiast
Jump to solution

thank you. but, it is not working as expected. Can you please re-look the script...

Reply
0 Kudos
jpsider
Expert
Expert
Jump to solution

can you please tell me what error you are getting?

Reply
0 Kudos
Mallik7
Enthusiast
Enthusiast
Jump to solution

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)

Reply
0 Kudos
jpsider
Expert
Expert
Jump to solution

I'm afraid you may not be able to remove it from a vm that is powered on. Here is some updated error handling.

$servernamelist = Get-Content -Path "C:\temp\servers.txt"


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"

    } 

}

Mallik7
Enthusiast
Enthusiast
Jump to solution

thank you so much. it is working as expected.

Reply
0 Kudos