Powershell Start-Process -NoNewWindow 不能与 -Credentials 一起使用

标签 powershell

我希望使用凭据从 powershell 运行 exe。我希望输出在同一个窗口中。这就是我的 powershell 的外观。
Start-Process文档:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6

$username = 'user'
$password = 'password'
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
$pathNew = "c:\pathtomyexe\text.exe" 
Start-Process $pathNew -NoNewWindow -Credential ($credentials) -PassThru -Wait

-Credential ($credentials)将启动一个新窗口。

当我跑 Start-Process没有 -Credential ,我在同一个窗口中得到了预期的结果。
Start-Process $pathNew -NoNewWindow -PassThru -Wait

我究竟做错了什么?任何指针?

最佳答案

简短的回答,你没有做错任何事。你只是无法做到这一点。

当您运行时 Start-Process -NoNewWindow没有 -Credential您说:使用当前已通过身份验证的凭据,运行可执行文件,并在同一控制台窗口中返回结果。

当您运行时 Start-Process-Credential第一个问题是:您如何验证凭证是否有效?您不能只查看用户名并假设您可以重新使用现有 session (例如密码可能错误)。要验证凭据,Start-Process以 Credential 对象中提供的用户名/密码启动一个新进程。它执行身份验证检查并获取新的身份验证票。

由于它是一个新进程,在全新的上下文下运行,具有新的身份验证票,因此它忽略了 -NoNewWindow标记,因为当前控制台无法重定向新进程的输出,并将其作为新窗口启动。

关于Powershell Start-Process -NoNewWindow 不能与 -Credentials 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56451094/

相关文章:

Powershell - "The process cannot access the file because it is being used by another process"

powershell - 将带有格式列表的哈希表转换回哈希表Powershell

bash - bash 管道中当前变量的名称

powershell - 获取休眠状态

powershell - 从 powershell 运行 Octave 脚本并等待其完成

powershell - 在 PowerShell 中从 C# 访问固定大小的数组元素

PowerShell 配置文件不安全?

通过管道传递路径时出现 Powershell 加入路径错误

powershell - Powershell和Visio出现错误

powershell - 确定 PowerShell 脚本是否已点源