# Does Remoting Run Synchronously

**URL:** <https://forums.powershell.org/t/does-remoting-run-synchronously/4298>\
**Category:** PowerShell Help\
**Created:** [June 2, 2015, 12:30am UTC](https://forums.powershell.org/t/does-remoting-run-synchronously/4298 "2015-06-02T00:30:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![terrance-copling](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@terrance-copling](https://forums.powershell.org/u/terrance-copling)\
**Post date:** [June 2, 2015, 12:30am UTC](https://forums.powershell.org/t/does-remoting-run-synchronously/4298/1 "2015-06-02T00:30:46Z")

</div>

I have a question about remoting; does stopping/starting a service on remote machines do they run synchronously? Meaning if I remotely start a service on Machine A and my code is set to next start a service remotely on Machine B; does the powershell engine wait untils the first service on Machine A is started then goes to Machine B or does it try to start them in parallel? or does it invoke the starting of the service on MachineA then proceeds to invoking the service on MachineB? Below is my code:

```
$computernames = "MachineA","MachineB"
  $servicestostart = "BITS","Spooler"

  $session = New-PSSession -ComputerName $computernames -Credential mydomain\username

  #list out the sessions
  Write-Host "`nCurrent Powershell sessions:"
  Get-PSSession

  #start my services
   for($i=0; $i -lt $computernames.Count; $i++)
   {
       $currentService = $servicestostart[$i]

       $Results = Invoke-Command -Session $session[$i] -ScriptBlock{
          Start-Service -Name $Using:currentService
       }
   }

  #display the services 
 for($i=0; $i -lt $computernames.Count; $i++)
   {
         $currentService = $servicestostart[$i]

         Invoke-Command -Session $session[$i] -ScriptBlock{
          Get-Service -Name $Using:currentService
       }
   }

Remove-PSSession -Session $session
Write-Host 'Completed...'
```

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 2, 2015, 12:46am UTC](https://forums.powershell.org/t/does-remoting-run-synchronously/4298/2 "2015-06-02T00:46:19Z")

</div>

When you run Invoke-Command, the shell will block (wait) until all computers mentioned in that Invoke-Command have either succeeded or failed. Those computers will run in parallel in a non-deterministic order, up to 32 simultaneously unless you modify -ThrottleLimit.

In your example (oh, Lord, the Write-Host pain), the “Start-Service” Invoke-Command would run for one computer at a time, in order, waiting for each one to succeed or fail before moving on to the next one. That’s because you’re manually looping through the sessions you created. Nothing will happen in parallel.

---

<div class="post-metadata">

**Author:** ![terrance-copling](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@terrance-copling](https://forums.powershell.org/u/terrance-copling)\
**Post date:** [June 2, 2015, 12:51am UTC](https://forums.powershell.org/t/does-remoting-run-synchronously/4298/3 "2015-06-02T00:51:48Z")

</div>

Thank you Mr. Jones for that answer. I suspected that was the case but wasn’t quite sure. Other than Write-Host; I’m assuming that Write-Output would be better?

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 2, 2015, 12:57am UTC](https://forums.powershell.org/t/does-remoting-run-synchronously/4298/4 "2015-06-02T00:57:08Z")

</div>

Well, no - it’s a bit more complicated than that. For what you’re doing, you probably want Write-Verbose, and set $VerbosePrefer=“Continue” at the top of the script. Write-Output is meant for your script’s pipeline output, so it can chain up to other commands. “Learn PowerShell Toolmaking in a Month of Lunches” kind of explains the entire pattern.

---

<div class="post-metadata">

**Author:** ![terrance-copling](https://avatars.discourse-cdn.com/v4/letter/t/94ad74/32.png) [@terrance-copling](https://forums.powershell.org/u/terrance-copling)\
**Post date:** [June 2, 2015, 1:07am UTC](https://forums.powershell.org/t/does-remoting-run-synchronously/4298/5 "2015-06-02T01:07:29Z")

</div>

Thank you. Last question, is it considered as good practice to do $VerbosePreference = “Continue” in the beginning and at the end of the script set it back to “SilentlyContinue”? and thanks for the suggested material.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 2, 2015, 1:14am UTC](https://forums.powershell.org/t/does-remoting-run-synchronously/4298/6 "2015-06-02T01:14:44Z")

</div>

You don’t need to set it back. When your script ends, the global version of the variable will take over again.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/does-remoting-run-synchronously/4298/7 "2024-05-16T20:45:22Z")

</div>

Closed on May 16, 2024, 8:45pm UTC


