-1

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.

Etaila
  • 218
  • 6
  • 24

1 Answers1

1

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}

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • This part is already included, I update my question to make it more clear what I am trying to do. – Etaila Nov 14 '16 at 09:57
  • 1
    no, you are running several invoke-command, but you should run it only once, like in the answer. why are you doing ``Invoke-Command -ScriptBlock {enter-pssession -ComputerName };``, this doesnt make sense – 4c74356b41 Nov 14 '16 at 09:58
  • I am sorry, I just now saw that your command was a direct Get-VM command to the server out of the current directory, I tried to first change directory to the directory of the VMs on the server and then use get-vm. I will now do it the other way round. – Etaila Nov 14 '16 at 10:22
  • glad it helped ;) – 4c74356b41 Nov 14 '16 at 10:27