4

In the below example, only the Get-Process in my scriptblock is being executed. "deh" does not print for some reason

Invoke-Command -ComputerName mycomputer -Credential $mycreds -ScriptBlock {
    Get-Process
    Write-Host "deh"
}

If I remove -ComputerName and execute on local, then it runs both commands just fine.

EDIT:

Here I am trying to execute IIS cmdlets against remote server. The following command works

Invoke-Command -ComputerName mycomputer -ScriptBlock { 
    Trace-Command CommandDiscovery {
        Import-Module webAdministration
        Start-WebAppPool -Name DefaultAppPool
    } -PSHost 
}

but this does not work

Invoke-Command -ComputerName mycomputer -ScriptBlock { 
    Import-Module webAdministration
    Start-WebAppPool -Name DefaultAppPool
}

what is special about Trace-Command that it is helping Start-WebAppPool to work? this is really odd and I can't explain why this functionality..

Eric Furspan
  • 742
  • 3
  • 15
  • 36
  • It executes both commands when removing the -ComputerName parameter, so it seems something to do with calling on remote computer – Eric Furspan May 03 '17 at 13:20

3 Answers3

3

No, the Invoke-Command cmdlet takes a scriptblock where you can put multiple commands. You should also be able to see the Write-Host output.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • Hmm, you are right. It seems to execute both commands when I remove the -ComputerName parameter and execute on local machine. Any idea for what reasons this would be? – Eric Furspan May 03 '17 at 13:01
  • `Write-host` writes to the console, so I think you wouldn't see it because it would occur within the remote session. – Mark Wragg May 03 '17 at 13:08
  • 4
    @MarkWragg You will get the output. Thought the same but tested it ;-) – Martin Brandl May 03 '17 at 13:09
1

You can do a Trace-Command on the remote machine via Invoke-Command to see what is happening. I'm not able to reproduce this.

Invoke-Command -ComputerName mycomputer -Credential $Creds -ScriptBlock { Trace-Command CommandDiscovery {get-process;write-host 'deh'} -PSHost }
Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26
  • something about Trace-Command is allowing my IIS Cmdlets to work against remote servers. But without it, they do not work. Really odd. I have updated OP. any ideas? – Eric Furspan May 05 '17 at 15:46
  • If you are in PS v 3.0 or later, you just try without Importing the WebAdministration module – Prasoon Karunan V May 07 '17 at 09:42
-2

Try to separate the commands using a semicolon:

  Invoke-Command -ComputerName mycomputer -Credential $mycreds -ScriptBlock {
        Get-Process ; 
        Write-Host "deh"
    }