How to run multiple commands via Invoke-Command on remote server

Eric Furspan picture Eric Furspan · May 3, 2017 · Viewed 7.2k times · Source

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..

Answer

Martin Brandl picture Martin Brandl · May 3, 2017

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.