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?
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
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
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.