windows Powershell 启动 notepad.exe 和一个命令窗口启动和消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25432348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:39:56  来源:igfitidea点击:

Powershell start notepad.exe and a command windows starts and disappears

windowspowershellcredentialsrunas

提问by user1548815

I have a script to start another ps1 script as aspecific user that does a simple dir on a directory and it works, when it runs a separate cmd window appears with the directory contents.

我有一个脚本可以作为特定用户启动另一个 ps1 脚本,该脚本在目录上执行一个简单的 dir 并且它可以工作,当它运行一个单独的 cmd 窗口时会出现目录内容。

$encpwd = Get-Content C:\path\to\creds.txt

$passwd = ConvertTo-SecureString $encpwd

$cred = new-object System.Management.Automation.PSCredential 'theuser',$passwd

Start-Process c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -Credential $cred  -ArgumentList '-noexit','-File', 'C:\path\to\script\test.ps1'

My goal is to start an application as a specific user. So first I wanted to test with notepad.exe So I changed the last line to:

我的目标是以特定用户身份启动应用程序。所以首先我想用 notepad.exe 测试所以我把最后一行改为:

Start-Process c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -Credential $cred  -ArgumentList '-noexit','-File', 'C:\windows\system32\notepad.exe'

All I get is a command window that quickly disappears, can't tell if there is an error etc. I also tried calling a ps1 script that invoked notepad and got the same result, even though on its own the the new script invoked notepad just fine.

我得到的只是一个快速消失的命令窗口,无法判断是否有错误等。我还尝试调用调用记事本的 ps1 脚本并得到相同的结果,即使新脚本本身只是调用了记事本美好的。

Can someone explain where I am going wrong, Thanks

有人可以解释我哪里出错了,谢谢

回答by CitizenRon

If you're wanting to start an application, wouldn't you just use that as the target of Start-Process instead of opening another PowerShell window to do it?

如果您想启动一个应用程序,难道您不只是将其用作 Start-Process 的目标,而不是打开另一个 PowerShell 窗口来执行此操作吗?

Start-Process 'C:\windows\system32\notepad.exe' -Credential $cred