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
Since you created the content with a Select-Object cmdlet.
That changes the type of object.
Try like this
$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
Since you created the content with a Select-Object cmdlet.
That changes the type of object.
Try like this
$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
Hi Luc,
It works.
Thank you
Best regards
Andi