VMware Cloud Community
vmstoani
Contributor
Contributor
Jump to solution

Disconnect CD Drive

Hi,

I'm trying to build a script which disconnect all mounted CD Drives in a specific Cluster.

When running this script, I get errors.

 

Script:

# Connection to vCenter
$cred = Get-VICredentialStoreItem -File "D:\Scripts\Credentials\credentials.xml"
Connect-VIServer -server $cred.host -User $cred.User -Password $cred.Password

# Get VMs of specific Cluster and search for VMs with CDDrive mounted
$vms = Get-Cluster -name xxxxxxx | Get-VM | Get-CDDrive | Where {$_.IsoPath -ne $null} | Select Parent

if($vms -ne $null) {

# Loop through VMs and unmount CDDrive
foreach ($vm in $vms) {
    Get-VM -name $vm | Get-CDDrive | Set-CDDrive -Connected:$false -NoMedia -Confirm:$false
    Write "Disconnecting CD Drive from VM $vm"
}
}
else {
Write "No CD-Drive mounted on VMs"
}

 

Error:

Disconnecting CD Drive from VM @{Parent=vm10152}
Get-VM : 21.02.2019 11:39:49 Get-VM  VM with name '@{Parent=as07813}' was not found using the specified filter(s).
At D:\Scripts\VM\DetachCdrom.ps1:12 char:5
+     Get-VM -name $vm | Get-CDDrive | Set-CDDrive -Connected:$false -N ...
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

 

So the Problem seems to be the VM Name. But why is it set into @{Parent=vmname}??

 

 

Thank's

 

best regards

Andi

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Since you created the content with a Select-Object cmdlet.
That changes the type of object.

Try like this

# Connection to vCenter

$cred = Get-VICredentialStoreItem -File "D:\Scripts\Credentials\credentials.xml"

Connect-VIServer -server $cred.host -User $cred.User -Password $cred.Password


# Get VMs of specific Cluster and search for VMs with CDDrive mounted

$vms = Get-Cluster -name STRETCHED-MS-ONLY | Get-VM | Get-CDDrive | Where {$_.IsoPath -ne $null} | Select -ExpandProperty Parent


if ($vms -ne $null) {


   # Loop through VMs and unmount CDDrive

   foreach ($vm in $vms) {

   Get-CDDrive -VM $vm | Set-CDDrive -Connected:$false -NoMedia -Confirm:$false

  Write "Disconnecting CD Drive from VM $vm"

   }

}

else {

  Write "No CD-Drive mounted on VMs"

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Since you created the content with a Select-Object cmdlet.
That changes the type of object.

Try like this

# Connection to vCenter

$cred = Get-VICredentialStoreItem -File "D:\Scripts\Credentials\credentials.xml"

Connect-VIServer -server $cred.host -User $cred.User -Password $cred.Password


# Get VMs of specific Cluster and search for VMs with CDDrive mounted

$vms = Get-Cluster -name STRETCHED-MS-ONLY | Get-VM | Get-CDDrive | Where {$_.IsoPath -ne $null} | Select -ExpandProperty Parent


if ($vms -ne $null) {


   # Loop through VMs and unmount CDDrive

   foreach ($vm in $vms) {

   Get-CDDrive -VM $vm | Set-CDDrive -Connected:$false -NoMedia -Confirm:$false

  Write "Disconnecting CD Drive from VM $vm"

   }

}

else {

  Write "No CD-Drive mounted on VMs"

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vmstoani
Contributor
Contributor
Jump to solution

Hi Luc,

It works.

Thank you

Best regards

Andi

0 Kudos