Win32-OpenSSH icon indicating copy to clipboard operation
Win32-OpenSSH copied to clipboard

How can I check if Microsoft.PowerShell_profile.ps1 is running from an SSH session?

Open eabase opened this issue 5 years ago • 2 comments

I'm trying to work around a bug where -NoProfile -NoLogo is not respected (#1677) when using pwsh.exe (Core) and logging remotely in via SSH. The way I tried to do it was by using the following in the very beginning of my Microsoft.PowerShell_profile.ps1 profile.

function IsInteractive {
    $non_interactive = '-command', '-c', '-encodedcommand', '-e', '-ec', '-file', '-f'
    -not ([Environment]::GetCommandLineArgs() | Where-Object -FilterScript {$PSItem -in $non_interactive})
}
# No point of running this script if not interactive
if (-not (IsInteractive)) {
    exit
}
...

However, this doesn't seem to work with a remote SSH, because when using [Environment]::GetCommandLineArgs() with pwsh.exe, all you get back is: C:\Program Files\PowerShell\6\pwsh.dll regardless whether or not you are in an "interactive" session.

Any better ideas how to check if the profile is running from a remote SSH session?


UPDATE: Apparently the *.dll thing is a bug in .NET 5.0 as reported here.

eabase avatar Oct 08 '20 18:10 eabase

This seem to be a hard cookie to crack. I tried to go through the process tree for process parents, but I could not find anything consistent to grasp on to. E.g. In one machine conhost starts before pwsh, whereas in another machine it starts after...then you need to scan up the tree and maybe find an explorer instance, in which case it is just a positive that the previous process is interactive, but not a definite non-interactive current process session. :confused:

eabase avatar Oct 15 '20 19:10 eabase

Bump.

eabase avatar Apr 21 '23 07:04 eabase