Question

I have a couple of lines in a script that are giving me an issue:

Connect-VIServer "test-vcenter.test.com" -User user -Password pass
Get-VM -Name "test-vm" | Get-Stat -Stat cpu.ready.summation -Realtime | Select-Object -First 1  value | Format-List

When running this I receive this as output:

Operation is not valid due to the current state of the object.
At :line:0 char:0

If the second line is run a few seconds after the connection to vCenter is made I receive the output I expect. What I believe is happening is that my connection to vCenter hasn't completed before my second line has started. I am not sure of the best way to wait for or what to check for in a completed connection.

Was it helpful?

Solution

you might try using start-sleep command in PS

http://technet.microsoft.com/en-us/library/ee177002.aspx

however -- as with all "sleep" functions, this isn't necessarily the best way to fix a timing problem as your pushback never may never account for all possible latency issues.

a better solution would be to test for the completion of a command (and I'm not sure how to do that with the VMWare CLI)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top