VMware Cloud Community
zenivox
Hot Shot
Hot Shot
Jump to solution

Get-VM throws error if inside Try function

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

Reply
0 Kudos
24 Replies
zenivox
Hot Shot
Hot Shot
Jump to solution

Thanks Luc, really appreciated! Tried version 11.5 and it's the same. I'll see to contact support... Cheers!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I just taled with someone from the PowerCLI Dev team.
We all tried to reproduce what you are seeing but to no avail.

It would help if you could capture the error with the Get-ErrorReport function., and attach that to your SR.

That function is documented in Improving PowerCLI Support with Get-ErrorReport

One way to capture the error is by running Get-VM in a ForEach loop x number of times.
Perhaps something like this (but make sure the error occurred at least once in that loop)

$errorCode = {
    $computer = '<VM-Name>'
    1..10 | ForEach-Object -Process {
        Get-VM -Name $computer
    }
  }
Get-ErrorReport -ProblemScript $errorCode -Destination . -ProblemDescription "Intermittent Get-VM issue"

 


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

Reply
0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

MANY THANKS AGAIN Luc! I captured the error with the GetErrorReport function. However I've uploaded a small video that shows how it happens... just for the sake of seeing this weird thing 🙂 

Reply
0 Kudos
zenivox
Hot Shot
Hot Shot
Jump to solution

also in the GSS web I can't find the PowerCLI listed in the products.. am I in the wrong place?

https://my.vmware.com/group/vmware/get-help?p_p_id=getHelp_WAR_itsupport&p_p_lifecycle=0&_getHelp_WA...

I followed the vmware support breakdown for PowerCLI but I have no choice to select from, and the Next button is greyed out as a consequence... 

zenivox_0-1609927920189.png

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The  PowerCLI Support Breakdown post states

  • Open a support request for the product area where the failure is occurring

so in your case, that would be System Management I guess.


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

Reply
0 Kudos