Please post your code and any errors that you’re receiving and maybe we can help you resolve this.
When posting code, data, and errors, please use the preformatted text </> button. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).
I have tried a lot of other ways / methods but I cannot get it working.
When I use the script-file alone with a parameter the server shuts down. I can see that in the vCenter.
I hadn’t tried the Receive-job.
It seems that I have no connection to the vCenter. When I move my “Connection-code” to the script, things seems to be working.
Something about the variable scope or what it is called.
Normally I connect to the vCenter after logging in and am passing the credentials to the vCenter.
It seems that when I use the “Scriptblock” I am in another scope. ???
When you run Start-Job it creates a new PowerShell session and your existing connection is not inherited.
Think of it this way:
You open a PowerShell window (session 1) and connect to vSphere.
You then open a second PowerShell window (session 2) and try to run Stop-VMGuest. It won’t work because session 2 is not aware of the connection you made in session 1.
While it’s not exactly the same, think of Start-Job as opening a second PowerShell window.