How to check if a Powershell script is running remotely

后端 未结 5 1893
渐次进展
渐次进展 2020-12-19 09:04

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

5条回答
  •  旧巷少年郎
    2020-12-19 09:35

    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
    

提交回复
热议问题