Hello, I'm experiencing a weird issue but most likely due to lack of knowledge on my side. See the few lines below:
$test1 = $true
Try {Get-VM -Name $computer}
Catch {$test1 = $false}
Finally {
if ($test1){
Write-Host "OK"
}
}
If I run just the Get-VM -Name $computer I get the right output. If I run the whole thing I get:
Get-VM : 12/23/2020 3:39:37 PM Get-VM VM with name 'testVM' was not found using the specified filter(s).
At line:3 char:10
+ Try {Get-VM -Name $computer}
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-VM], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
OK
there must be something with the Try function that I don't know. However if I use the $computer variable inside other functions it works. Also... shouldn't the Catch set $test1 to false and then the "OK" message should not come out?
My environment:
PS v.5.1
PowerCLI Version
----------------
VMware PowerCLI 12.1.0 build 17009493
---------------
Component Versions
---------------
VMware Common PowerCLI Component 12.1 build 16997174
VMware Cis Core PowerCLI Component PowerCLI Component 12.1 build 16997582
VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 12.1 build 16997984