title: PowerShell Invoke-Command Returns Blank Data?
body: <p>Been trying to solve this for a bit and can't seem to figure it out.</p>
  
  <p>I have the following script:</p>
  
  <pre><code>$Servers = Get-Content -Path "C:\Utilities_PowerShell\ServerList.txt"
  $IISServiceName1 = 'W3SVC'
  $IISServiceName2 = 'IISAdmin'
  $IISServiceName3 = 'WAS'
  $IISarrService = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3
  $IISarrServiceCheck = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 -ErrorAction SilentlyContinue -ErrorVariable NoService
  
  function IISServiceStatus # Checks for status of IIS services
  {
      param (
      $IISServiceName1,
      $IISServiceName2,
      $IISServiceName3,
      $IISarrService,
      $IISarrServiceCheck
      )
  
      if (Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3)
      {
          Write-Host "Status of IIS service(s) on $env:ComputerName :"
          Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 | Select Name,DisplayName,Status | Format-Table -AutoSize
      }
      else
      {
          Write-Host "  No IIS service(s) were found..." -foreground "red"
      }
  }
  
  $Sessions = New-PSSession -ComputerName $Servers
  $EndJobs = $Sessions | ForEach-Object {
      Invoke-Command -Session $_ -ScriptBlock ${function:IISServiceStatus} -AsJob -ArgumentList $IISServiceName1, $IISServiceName2, $IISServiceName3, $IISarrService, $IISarrServiceCheck | Wait-Job | Receive-Job
      Write-Host " "
  }
  </code></pre>
  
  <p>Whenever I run it, all I get is the output of:</p>
  
  <pre><code>  Status of IIS service(s) on *PC* :
  </code></pre>
  
  <p>If I run the function outside of a loop/invoke-command, the results are absolutely perfect. What is wrong with my remote loop?</p>
  
  <p>I've tried putting the variables inside the function, I've tried running invoke-command without the argument list, etc.</p>
  
  <p><strong>Update: 3/17/16</strong></p>
  
  <p>Turns out...if I run my actual script as is, the result of <code>$EndJobs</code> is weird in that it outputs ALL services in one table and then the three IIS services in another table. This would explain why when I run my invoke-command (stopIIS) scriptblock...I had to reboot the whole server because it took all of the services down.</p>
  
  <p>These functions run PERFECTLY when not run via remote/invoke-command.</p>
  
  <p>What the heck...invoke-command is seriously screwing with my stuff!</p>
  
  <p>Anyone have any ideas/tips on how I can run my local script (which works 100%) on a set of servers from a text file without weird issues like this? Is invoke-command the only way?</p>