0

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?

3
  • What is $EndJobs value after invoking your code? Commented Mar 16, 2016 at 18:13
  • 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. Commented Mar 16, 2016 at 20:20
  • 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! Commented Mar 17, 2016 at 14:23

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

This worked, @Anthony Stringer. Can you explain why? Basically, I invoke this function at various times in my script (my script is actually quite massive, this is a tiny 1% snippet of it)...my other functions, which are called with simple invoke-command $(function:name) commands have no issues running...just this particular IISStatus one...
i wish i could, but i am not familiar with ${function:IISServiceStatus}. perhaps someone else can explain the issue.
Still appreciate it...worst case I'll use it although it's not necessarily a clean looking solution. Thank you for taking the time! @Anthony Stringer.
0

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
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.