PowerShell Core does not detect non-interactive user session correctly

Created on 31 Oct 2018  路  7Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Run PowerShell script from Jenkins or another Windows service.

bat 'pwsh -File C:\\Temp\\Test.ps1 -NonInteractive -NoProfile'
bat 'powershell -File C:\\Temp\\Test.ps1 -NonInteractive -NoProfile'

The NonInteractive option changes behavior of some cmdlets (Read-Host, Get-Credential), but it's not used to detect if user is able to type text in terminal. It's fine.

Test.ps1:

"UserInteractive: " + [Environment]::UserInteractive

$s = New-PSSession -UserName JohnDoe -HostName example.com
Invoke-Command -Session $s -ScriptBlock { hostname }
Remove-PSSession $s

Expected behavior

PowerShell works correctly, UserInteractive value is False.

UserInteractive: False
New-PSSession : A parameter cannot be found that matches parameter name 
'UserName'.

Actual behavior

PowerShell Core does not detect non-interactive user session. The SSH tool hangs with 'Are you sure you want to continue connecting (yes/no)?' question.

UserInteractive: True

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Microsoft Windows 6.3.9600
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Discussion Resolution-External WG-Remoting Waiting - DotNetCore

Most helpful comment

.NET Core is the culprit: [Environment]::UserInteractive currently _always_ returns $True (hard-coded):

https://github.com/dotnet/corefx/blob/a10890f4ffe0fadf090c922578ba0e606ebdd16c/src/System.Runtime.Extensions/src/System/Environment.cs#L152

(The platform-specific class files do not override this behavior.)

All 7 comments

[Environment]::UserInteractive is a bit misleading. It checks if the process is running in a winstation (has a desktop). Windows services don't have a winstation, otherwise it is considered "UserInteractive". The -noninteractive switch for pwsh controls whether the console host prompts (basically if ReadLine() is needed). It has no control over native commands that prompt (or cmdlets/scripts that call [Console]::ReadLine() directly. New-PSSession uses ssh.exe under the covers which is what is providing that prompt. Not sure if the cmdlet can detect if pwsh was run with -noninteractive and even then what it can do about the ssh.exe prompt.

@SteveL-MSFT
I understand that -Noninteractive and [Environment]::UserInteractive are not linked at all.

Don't you think it's strange that [Environment]::UserInteractive returns different value for PowerShell and PowerShell Core?

even then what it can do about the ssh.exe prompt

I assume that pwsh redirects standard input. It reads a response from user and sends it to ssh.exe. I think pwsh may disable stdin if there is no "winstation" and input stream is not redirected.

@dermeister0 so you are saying Windows PowerShell returns false?

.NET Core is the culprit: [Environment]::UserInteractive currently _always_ returns $True (hard-coded):

https://github.com/dotnet/corefx/blob/a10890f4ffe0fadf090c922578ba0e606ebdd16c/src/System.Runtime.Extensions/src/System/Environment.cs#L152

(The platform-specific class files do not override this behavior.)

@dermeister0: If you're willing to define _user-interactive_ as _runs in a console/terminal window visible to the current user_, see this answer on StackOverflow.com for a workaround.

Migrating from 5.1 to 7 Preview and encountered this problem.
I used [Environment]::UserInteractive in 5.1 to know if the script is running from Scheduled Task or not.
The .Net issue is tracked here https://github.com/dotnet/coreclr/issues/25595
The workaround suggested there is to use this instead:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SystemInformation]::UserInteractive

Would be nice if [Environment]::UserInteractive will work like it did in 5.1

This issue has been marked as external and has not had any activity for 1 day. It has been be closed for housekeeping purposes.

Was this page helpful?
0 / 5 - 0 ratings