【问题标题】:Powershell start notepad.exe and a command windows starts and disappearsPowershell 启动 notepad.exe 和命令窗口启动并消失
【发布时间】:2014-08-21 17:25:59
【问题描述】:

我有一个脚本可以以特定用户身份启动另一个 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 脚本并得到相同的结果,即使它自己是新脚本调用记事本就好了。

谁能解释我哪里出错了,谢谢

【问题讨论】:

    标签: windows powershell credentials runas


    【解决方案1】:

    如果你想启动一个应用程序,你不会直接将它用作 Start-Process 的目标,而不是打开另一个 PowerShell 窗口来执行它吗?

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

    【讨论】:

    • 你成功地解开了我的版本的混乱,它有时让其他人穿过树木有助于看到森林。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2012-09-26
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2010-10-13
    相关资源
    最近更新 更多