我希望使用凭据从 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/