我有一个脚本可以作为特定用户启动另一个 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'
我的目标是以特定用户身份启动应用程序。所以首先我想用 notepad.exe 进行测试所以我将最后一行更改为:
Start-Process c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -Credential $cred -ArgumentList '-noexit','-File', 'C:\windows\system32\notepad.exe'
我得到的只是一个快速消失的命令窗口,无法判断是否有错误等。我还尝试调用一个调用记事本的 ps1 脚本并得到相同的结果,即使新脚本本身调用记事本就好了。
谁能解释一下我哪里出错了,谢谢
最佳答案
如果您想要启动一个应用程序,难道您不会直接将其用作 Start-Process 的目标而不是打开另一个 PowerShell 窗口来执行它吗?
Start-Process 'C:\windows\system32\notepad.exe' -Credential $cred
关于windows - Powershell 启动 notepad.exe 并且命令窗口启动并消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25432348/