VMware Cloud Community
psautter
Contributor
Contributor
Jump to solution

Removing all media in cd-drives that target a datastore

I am attempting to write part of a script where I search for all VMs that have an image in the cd-drive the points to a certain datastore and then removes that media.

This is what I have come up with, and I think it will work.

$datastore = "iso" # Datastore that will be removed
Get-VM | where{$_ | Get-CDDrive | where{$_.IsoPath -match $datastore }} | Export-Csv drive.csv #Gets a list of all VMs with CDs attached to that datastore
$egress = Import-Csv ".\drive.csv"
foreach ($_.Name in $egress) {
Get-VM -Name $_.Name | Get-CDDrive | Set-Cddrive -NoMedia} #Removes all media in cd-drives 

I feel like i cheated by exporting to a csv file, but when i tried to do a "|Select Name" the value I got was something like "@{Name=XYZ}".

Any advice on how I could clean this up or make it more efficient?

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi,

welcome to the VMware Communites.

The next script is a cleaned version of your script:

$datastore = "iso" # Datastore that will be removed
Get-VM | Get-CDDrive | where {$_.IsoPath -match $datastore } | Set-Cddrive -NoMedia

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi,

welcome to the VMware Communites.

The next script is a cleaned version of your script:

$datastore = "iso" # Datastore that will be removed
Get-VM | Get-CDDrive | where {$_.IsoPath -match $datastore } | Set-Cddrive -NoMedia

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
psautter
Contributor
Contributor
Jump to solution

Thanks Robert. I figured it could be done in a lot less characters than I was attempting to doing it, but for some reason I didn't think of doing it that way.

Reply
0 Kudos