0
votes

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?

2
What is $EndJobs value after invoking your code? - user4003407
The value is correct, @PetSerAl, if I output $EndJobs after running my code. As I stated below, I have a bunch of other functions called using the same invoke-command method and they run fine...the only one that didn't was this one. I don't understand why this one won't output the results like all of the others do. - Ilya
Actually, @PetSerAl, I stand corrected. 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) script 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! - Ilya

2 Answers

1
votes

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
0
votes

I'm having a similar issue. I'm using credssp to test 2nd hop auth for an automation for shutting down a production environment cleanly. My script has 3 sections; session setup, the invoke, session teardown. If I run each piece separately, I get output. If I run the whole script, I get blank lines matching the amount of output I get when I run them separately... there's nothing fancy in my invoke (backtick line continuation - I prefer Python's formatting paradigm better than Powershell/C#):

Invoke-Command                      `
    -Session        $workingSession `
    -ScriptBlock    {
        get-service *spool* -ComputerName server01
    }