Search code examples
powershellconnectionwaitpowershell-5.0

Powershell: waiting for changed directory


To make it short, I want to connect to a server that is running Virtual Machines and then get a List of all installed machines, the command I use for this is:

Invoke-Command -ScriptBlock {enter-pssession -ComputerName <name>}; Invoke-Command -ScriptBlock {Get-VM} | select-Object -Property name

This line contains two commands at first:

Invoke-Command -ScriptBlock {enter-pssession -ComputerName <name>};

this part connects to the server, and then:

Invoke-Command -ScriptBlock {Get-VM} | select-Object -Property name

This command gets a list of the VMs currently on the server and returns specific properties of these servers.

However, because the connection needs a short time until it is set up, the "get-vm" command is still set in the previous direction and results in an error report.

I want to know if there is a way to wait for ether a command to be finished or for a change in the directory, without having an extra loop running for this time, or waiting for a hard set time.


Solution

  • I don't know why are you trying to do what you are trying to do, what you should do is:

    Invoke-Command -SessionName (or -ComputerName) -ScriptBlock {Get-VM | Select-Object -Property name}