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

标签 windows powershell credentials runas

我有一个脚本可以作为特定用户启动另一个 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/

相关文章:

windows - Signtool.exe 成功,但向文件添加了零个签名

powershell - 如何将不同的 Powershell 宏合并为一个?

powershell - 如何让 PowerShell 获取 "see".NET 枚举值?

azure - 如何使用 Powershell 从 csv 文件中仅读取一列

jenkins - 从 Jenkins 凭证管理器导出 keystore

c++ - 如何知道文件何时被编辑?

windows - 我将如何转换字符?或者其他解决方案

windows - 如何与 Windows 8.1 应用一起发布 UWP 应用?

c# - 添加服务引用,多凭证提示

credentials - 以编程方式将 secret 附加到credentials.yml.enc