【问题标题】:From Powershell, how do you launch an executable in administrator mode under a different user context?在 Powershell 中,如何在不同的用户上下文中以管理员模式启动可执行文件?
【发布时间】:2014-02-08 02:20:01
【问题描述】:

我猜测会使用 Start-Process,但我不确定如何同时完成这两件事。

用户是本地用户并且是管理员的成员。

此外,可能不需要用户输入。

更新:这是我尝试过的以及为什么它不起作用的原因。 正如基思建议的那样,我尝试执行以下操作:

$passwd = ConvertTo-SecureString -AsPlainText "opensesame" -Force
$cred = new-object system.management.automation.pscredential 'John',$passwd
Start-Process powershell.exe -credential $cred

这不会在管理员模式下启动 powershell 窗口。添加runas动词不起作用,因为start-process有two completely separate ways to call it,其中一个以-Verb为参数,其中一个以-Credential:

Start-Process [-FilePath] <string> [[-ArgumentList] <string[]>] [-Credential <PSCredential>] [-LoadUserProfile] [-NoNewWindow] [-PassThru] [-RedirectStandardError <string>] [-RedirectStandardInput <string>] [-RedirectStandardOutput <string>] [-UseNewEnvironment] [-Wait] [-WorkingDirectory <string>] [<CommonParameters>]

Start-Process [-FilePath] <string> [[-ArgumentList] <string[]>] [-PassThru] [-Verb <string>] [-Wait] [-WindowStyle {<Normal> | <Hidden> | <Minimized> | <Maximized>}] [-WorkingDirectory <string>] [<CommonParameters>]

这意味着以下返回错误:

Start-Process powershell.exe -credential $cred -verb runas

【问题讨论】:

    标签: powershell


    【解决方案1】:

    试试这个:

    $passwd = ConvertTo-SecureString -AsPlainText "opensesame" -Force
    $cred = new-object system.management.automation.pscredential 'John',$passwd
    Start-Process powershell.exe -credential $cred
    

    TechNet article 包含有关使用凭据的脚本的更多信息。

    【讨论】:

    • 是的,忘了这两个参数是互斥的。从答案中删除了那一点。我会再调查一下。我认为这个系统上启用了 UAC?
    • 是的。这是在 Azure Worker Role 上运行的,这就是它具有严格的自动化和用户要求的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多