Mallik7
Enthusiast
Enthusiast

In need of a PowerCLI script

Jump to solution

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

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

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

foreach ($vmname in $servernamelist){   

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

}

0 Kudos
Mallik7
Enthusiast
Enthusiast

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

0 Kudos
jpsider
Expert
Expert

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

     }  

}

0 Kudos
jpsider
Expert
Expert

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

0 Kudos
Mallik7
Enthusiast
Enthusiast

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

0 Kudos
jpsider
Expert
Expert

can you please tell me what error you are getting?

0 Kudos
Mallik7
Enthusiast
Enthusiast

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)

0 Kudos
jpsider
Expert
Expert

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

Mallik7
Enthusiast
Enthusiast

thank you so much. it is working as expected.

0 Kudos