父 PowerShell 控制台似乎控制子 PowerShell 控制台 I/O [英] Parent PowerShell Console Seems to Control Child PowerShell Console I/O

查看:49
本文介绍了父 PowerShell 控制台似乎控制子 PowerShell 控制台 I/O的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$PSVersionTable 输出是

名称值---- -----PS版本 5.1.18362.628PS版桌面PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}构建版本 10.0.18362.628CLR 版本 4.0.30319.42000WSManStackVersion 3.0PSRemotingProtocolVersion 2.3序列化版本 1.1.0.1

[System.Environment]::OSVersion.Version 输出为

Major Minor Build Revision----- ----- ----- --------10 0 18363 0

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 作为本地管理员组成员的用户启动,但不是在提升模式下.将此称为父"控制台.

在父控制台中,执行(其中用户名"应替换为以交互方式登录到计算机的相同或不同用户)

Start-Process -FilePath powershell.exe -Credential UserName

为用户名"输入正确的密码后,将打开一个新的 PowerShell 窗口(我们称之为子"控制台).在其中,键入退出",然后按 Enter/Return 键.子控制台中没有任何反应:没有字母出现,不处理 Enter/Return.

切换到父控制台,按四次空格键.当按下空格键时,退出"将出现在子控制台中.再按一次空格键将导致在子进程中处理Enter/Return"键,然后退出.

我已经在另一台运行 2016 版(10.0 版本 14393)的 Win 10 的机器上尝试过,结果相同.

这似乎不对.这样对吗?如果是,我该如何避免?

谢谢.

解决方案

这似乎不对.对吗?

不,不幸的是,这是一个长期存在的错误,从 PowerShell 7.0 开始仍然存在;它已在此 GitHub 问题 中报告.

JosefZ 在评论中缩小了范围:

<块引用>

似乎两个进程(通过描述的过程创建)都阻止键盘输入相互.如果我使用 -Wait 开关,那么至少 child 进程接受键盘输入...

<小时><块引用>

我该如何避免?

解决方法是使用标准的runas.exe 实用程序:

runas/user:UserName powershell.exe

警告:正如您所指出的,HOMEPATH(可能还有 HOMEDRIVE)环境变量在新会话中设置不正确(从 Windows 10 版本 1909 开始):HOMEPATH 意外指向 \WINDOWS\system32 而不是用户的主路径;相比之下,USERPROFILE 确实包含预期值.

$PSVersionTable output is

Name                           Value
----                           -----
PSVersion                      5.1.18362.628
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.628
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

[System.Environment]::OSVersion.Version output is

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      18363  0

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe started as a user who is a member of the local administrator's group, but not in elevated mode. Call this the 'parent' console.

In the parent console, execute (where 'UserName' should be replaced by either the same or a different user logged-in interactively to the computer)

Start-Process -FilePath powershell.exe -Credential UserName

Upon entering the correct password for 'UserName', a new PowerShell window opens (let's call this the 'child' console). Within that, type 'exit' and then hit the Enter/Return key. Nothing happens in the child console: no letters appear, the Enter/Return is not processed.

Switch to the parent console, press the space bar four times. As the space bar is pressed, 'exit' will appear in the child console. Pressing the space bar one more time will cause the 'Enter/Return' key to be processed in the child and it will then exit.

I've tried this on another machine running a 2016 version (10.0 build 14393) of Win 10 with the same results.

This doesn't seem right. Is it right? If it is, how do I avoid it?

Thank you.

解决方案

This doesn't seem right. Is it right?

No, it's a long-standing bug, still present as of PowerShell 7.0, unfortunately; it has been reported in this GitHub issue.

JosefZ narrowed things down in a comment:

It seems that both processes (created via described procedure) are blocking keyboard input mutually. If I use the -Wait switch, then at least the child process accepts keyboard input…


how do I avoid it?

The workaround is to use the standard runas.exe utility:

runas /user:UserName powershell.exe

Caveat: As you point out, the HOMEPATH (and presumably also HOMEDRIVE) environment variable isn't set correctly in the new session (as of Windows 10, version 1909): HOMEPATH unexpectedly points to \WINDOWS\system32 rather than the user's home path; by contrast, USERPROFILE does contain the expected value.

这篇关于父 PowerShell 控制台似乎控制子 PowerShell 控制台 I/O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆