Powershell启动notepad.exe并且命令窗口启动并消失

时间:2014-08-21 17:25:59

标签: windows powershell credentials runas

我有一个脚本可以启动另一个ps1脚本作为特定用户在目录上执行一个简单的目录,并且当它运行一个单独的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'

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

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

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

有人可以解释我出错的地方,谢谢

1 个答案:

答案 0 :(得分:9)

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

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