title: How to loop through Multiple Machines in a PSSession such that I execute Invoke-Command against the correct Machine?
body: <p>My Powershell Script below wants to retrieve multiple registry values, from a set of Windows Services (200+), from multiple machines (up to 8 machines). Since I'm retrieving quite a lot of info, I decided to use <code>New-PSSession</code>.</p>
  <p>But whenever it gets into the <code>scriptblock</code> of the <code>foreach</code> loop, it executes it on the incorrect machine and thus eventually errors out saying</p>
  <p><strong>The Error:</strong></p>
  <pre><code>Cannot find path 'HKLM:\system\currentcontrolset\services\SomeServiceName' because it does not exist.
      + CategoryInfo          : ObjectNotFound: (HKLM:\system\cu...ces\SomeServiceName:String) [Get-ItemProperty], ItemNotFoundException
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
      + PSComputerName        : WEB002
  </code></pre>
  <p><strong>The Powershell Script:</strong></p>
  <pre><code>$MyCustomServiceArray = @()
  $c1 = 0
  $s = New-PSSession -ComputerName WEB001, WEB002 
  $MyCustomServices = Invoke-Command -session $s -scriptblock {Get-WmiObject -Class win32_service |
                      Where-Object -FilterScript {$_.Displayname -like &quot;*Special Service for Me*&quot;} |
                      Select PSComputerName, Name, DisplayName, State, StartMode, StartName, PathName |
                      Sort DisplayName}
  
  
  foreach($MyCustomService in $MyCustomServices)
  {
      $MyCustomServiceName = $MyCustomService.Name
      $RegistryPathForService = &quot;hklm:\system\currentcontrolset\services\$MyCustomServiceName&quot;
      $c1 += 1
      Write-Host &quot;$c1. $MyCustomServiceName`n&quot;
      
      $StartUpParameter = Invoke-Command -session $s -scriptblock {(Get-ItemProperty $using:RegistryPathForService).startupparameter}
      if ($StartUpParameter -eq $null) {$StartUpParameter = &quot;&quot;}
      
      $DatabaseServerName = Invoke-Command -session $s -scriptblock {(Get-ItemProperty $using:RegistryPathForService).servername}
      if ($DatabaseServerName -eq $null) {$DatabaseServerName = &quot;&quot;}
      
      $DatabaseName = Invoke-Command -session $s -scriptblock {(Get-ItemProperty $using:RegistryPathForService).database}
      if ($DatabaseName -eq $null) {$DatabaseName = &quot;&quot;}
      
      $NetType = Invoke-Command -session $s -scriptblock {(Get-ItemProperty $using:RegistryPathForService).nettype}
      if ($NetType -eq $null) {$NetType = &quot;&quot;}
      
      $ObjectCacheSize = Invoke-Command -session $s -scriptblock {(Get-ItemProperty $using:RegistryPathForService).objectcache}
      if ($ObjectCacheSize -eq $null) {$ObjectCacheSize = 0}
      
      $Row = &quot;&quot; | Select HostName,ServiceName,StartUpParameter,ServiceDisplayName,Status,StartUpType, `
                         DatabaseServerName,DatabaseName,NetType,ObjectCacheSize, `
                         LogOnName,PathName
      $Row.HostName           = $MyCustomService.PSComputerName
      $Row.ServiceName        = $MyCustomService.Name
      $Row.StartUpParameter   = $StartUpParameter
      $Row.ServiceDisplayName = $MyCustomService.DisplayName
      $Row.Status             = $MyCustomService.State
      $Row.StartUpType        = $MyCustomService.StartMode
      $Row.DatabaseServerName = $DatabaseServerName
      $Row.DatabaseName       = $DatabaseName
      $Row.NetType            = $NetType
      $Row.ObjectCacheSize    = $ObjectCacheSize  
      $Row.LogOnName          = $MyCustomService.StartName
      $Row.PathName           = $MyCustomService.PathName
      $MyCustomServiceArray += $Row
  }
  $MyCustomServiceArray | Export-Csv -NoTypeInformation -Path &quot;C:\Users\GaryTheBrave\Documents\AllMyCustomServices.csv&quot;
  
  </code></pre>
  <p>Is there a way to loop through these Multiple Machines in the correct order?</p>