Hi,
I am using the below script to validate if the VM exists, if the VM is valid, the script works fine but for invalid VMs, it is not checking nor I am getting the error message which I defined in the script, directly, I am getting error
$report = @()
$report = Import-Csv ".\VMNotes0.csv"
foreach ($VMAttrib in $report){
if (!$VMAttrib) {
Write-Host "No VM named $VM exists" }
else
{Get-VM $VMAttrib.Name | Set-VM -Notes $VMAttrib.Notes -confirm:$false}
}
Error :
Get-VM : 09-08-2019 17:49:16 | Get-VM | VM with name 'MyDB10' was not found using the specified filter(s). |
At D:\export\3.ps1:9 char:2
+ {Get-VM $VMAttrib.Name | Set-VM -Notes $VMAttrib.Notes -confirm:$fals ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo | : ObjectNotFound: (:) [Get-VM], VimException | |
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM |
Try something like this
try{
Get-VM -Name $row.Name -ErrorAction Stop |
Set-VM -Notes $row.Notes -Confirm:$false
}
catch{
Write-Host "No VM named $($row.Name) exists"
}
}
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
try{
Get-VM -Name $row.Name -ErrorAction Stop |
Set-VM -Notes $row.Notes -Confirm:$false
}
catch{
Write-Host "No VM named $($row.Name) exists"
}
}
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked LucD. Thank you very much