Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if I'm in a remote PowerShell session?

I would like to execute code that is specific to remote PSSessions. That is, the code doesn't apply locally, but applies to all remote sessions.

Is there any environment variable, function or cmdlet that would effectively return true if I'm in an active PSSession and false if I'm running locally?

like image 229
Kris Harper Avatar asked Dec 31 '12 15:12

Kris Harper


People also ask

How do I know if PowerShell is remoting?

Check PowerShell Remoting is enabledWhen you run the Test-WSMan command on a local computer then you can see if PowerShell Remoting is enabled or not. Of course, you can run the command for another computer by using the -ComputerName parameter.

What is a remote PowerShell session?

Using the WS-Management protocol, Windows PowerShell remoting lets you run any Windows PowerShell command on one or more remote computers. You can establish persistent connections, start interactive sessions, and run scripts on remote computers.

How do I know if PSSession is enabled?

Just run Enter-PSSession -ComputerName localhost. If it enters the remote session, PS remoting is enabled.

Is remote PowerShell enabled?

PowerShell remoting is enabled by default on Windows Server platforms. You can use Enable-PSRemoting to enable PowerShell remoting on other supported versions of Windows and to re-enable remoting if it becomes disabled. You have to run this command only one time on each computer that will receive commands.


1 Answers

Check if the $PSSenderInfo variable exists. From about_Automatic_Variables:

$PSSenderInfo

Contains information about the user who started the PSSession, including the user identity and the time zone of the originating computer. This variable is available only in PSSessions.

The $PSSenderInfo variable includes a user-configurable property, ApplicationArguments, which, by default, contains only the $PSVersionTable from the originating session. To add data to the ApplicationArguments property, use the ApplicationArguments parameter of the New-PSSessionOption cmdlet.

like image 91
Shay Levy Avatar answered Oct 01 '22 03:10

Shay Levy