我有一个脚本,可以以特定用户的身份启动另一个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脚本并获得了相同的结果,即使新脚本本身单独调用了记事本也是如此。精细。

有人可以解释我要去哪里了吗,谢谢

最佳答案

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

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

07-28 00:58
查看更多