Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 300
Eric Furspan Avatar asked Nov 01 '25 12:11

Eric Furspan


1 Answers

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.

like image 169
Martin Brandl Avatar answered Nov 03 '25 18:11

Martin Brandl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!