8

I have a script that can be run either locally or remotely (via WinRM), however I would like it to behave slightly differently when run on a remote machine. I realise that I can pass in a switch to the script to identify whether it is running locally or remotely, but I want to know if it is possible for the script itself to detect whether it is running remotely?

Paul Bevis
  • 831
  • 1
  • 8
  • 16

5 Answers5

12

Get-Host returns, amongst other information a Name:

PS> (Get-Host).Name
ConsoleHost
PS> (Invoke-Command -ComputerName dev2 -Script {Get-Host}).Name
ServerRemoteHost
Richard
  • 106,783
  • 21
  • 203
  • 265
  • 1
    Nice. Using automatic variable `$Host` in lieu of cmdlet call `Get-Host` is also an option: `$Host.Name -eq 'ServerRemoteHost'`. – mklement0 Mar 26 '18 at 14:08
  • I don't see how this helps unless you're passing in the name of the "local" computer so you can compare against it ... – Jaykul Aug 13 '18 at 18:30
  • @Jaykul The PowerShell host is the software executable running the script; not the host name of the computer running the process. Hence `ConsoleHost` etc: these are names that show the execution environment. – Richard Aug 14 '18 at 07:13
6
    if ($PSSenderInfo) {
        "Running remote"
    }
    else {
        "Running local"
    }
Allan
  • 61
  • 1
  • 1
  • 1
    Nice. To explain why that works: [automatic variable `$PSSenderInfo` is "available only in PSSessions"](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables). – mklement0 Mar 26 '18 at 14:06
  • This seems like a better answer than the expected answer, since it only works in PSSessions -- but it's worth noting that it *does* work in `Invoke-Command -Cn . { $PSSenderInfo }` so it only tells you if you're _remoting_, and not technically whether you're on a remote computer. – Jaykul Aug 13 '18 at 18:28
  • Now a question remain. How do you get and intsall `PSSessions`? – not2qubit Dec 21 '20 at 19:50
1

Checking whether $profile is defined works for me. I don't know how reliable this is, but various sources suggest that no profile is loaded in the remote session. I couldn't check the computername, as I don't have any way of knowing in advance which machine is local and which is remote.

$RunningLocally = $profile -ne $null
Community
  • 1
  • 1
Rob
  • 4,327
  • 6
  • 29
  • 55
0

you can check computername in $MyInvocation :

PS>Invoke-Command -ComputerName smacnt01 -ScriptBlock {$MyInvocation}

PSComputerName   : smacnt01
RunspaceId       : 333f5333-0ca5-4461-8f38-cb086fbd1ea4
MyCommand        : $MyInvocation
BoundParameters  : {}
UnboundArguments : {}
ScriptLineNumber : 0
OffsetInLine     : 0
HistoryId        : -1
ScriptName       :
Line             :
PositionMessage  :
InvocationName   :
PipelineLength   : 1
PipelinePosition : 1
ExpectingInput   : False
CommandOrigin    : Runspace
Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • Thanks for the response Kayasax, not sure how I will be able to use this to tell if the script is running remotely though. Unless there is some way for the script to get the name of the computer that initiated the remote call in order to compare it against the value in $MyInvocation? – Paul Bevis Nov 15 '12 at 09:55
  • in the script where you start the invoke-command you can call $env:computername no ? – Loïc MICHEL Nov 15 '12 at 09:58
  • That would work but I wanted to avoid passing extra values through to the script and instead have the script figure it out – Paul Bevis Nov 15 '12 at 10:10
  • in $Myinvocation CommandOrigin seems to be 'internal' when running localy instead of Runspace when using remoting ( you got to verify this) – Loïc MICHEL Nov 15 '12 at 10:13
0

I think it's possible in your script test if

$myinvocation.mycommand.path -eq $null

then if is true is running remotely via Invoke-command.

I have not test it accurately but I think it can work.

CB.
  • 58,865
  • 9
  • 159
  • 159