Search code examples
powershellinvoke-command

PowerShell Invoke-Command Returns Blank Data?


Been trying to solve this for a bit and can't seem to figure it out.

I have the following script:

$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 " "
}

Whenever I run it, all I get is the output of:

  Status of IIS service(s) on *PC* :

If I run the function outside of a loop/invoke-command, the results are absolutely perfect. What is wrong with my remote loop?

I've tried putting the variables inside the function, I've tried running invoke-command without the argument list, etc.

Update: 3/17/16

Turns out...if I run my actual script as is, the result of $EndJobs 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.

These functions run PERFECTLY when not run via remote/invoke-command.

What the heck...invoke-command is seriously screwing with my stuff!

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?


Solution

  • do you have the same problem if you wrap it all into the script block like this?

    $Servers = Get-Content 'C:\Utilities_PowerShell\ServerList.txt'
    
    $Sessions = New-PSSession -ComputerName $Servers
    
    $EndJobs = $Sessions | ForEach-Object {
        Invoke-Command -Session $_ -ScriptBlock {
            $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...' -ForegroundColor Red
                }
            }
    
            IISServiceStatus $IISServiceName1 $IISServiceName2 $IISServiceName3 $IISarrService $IISarrServiceCheck
        } -AsJob | Wait-Job | Receive-Job
        Write-Host ' '
    }
    
    $EndJobs