【问题标题】:Running a .bat File as Admin from Powershell从 Powershell 以管理员身份运行 .bat 文件
【发布时间】:2017-02-12 11:02:42
【问题描述】:

所以我一直在这一个上旋转我的轮子。我正在 powershell gui 中创建一个小型应用程序,它需要通过传递从文本框中输入的凭据来运行 .bat。我一直想弄清楚当在 GUI 上单击按钮时如何传递这些凭据。

我在 GUI 上有两个框,用户在其中传递凭据。

$userTextBox.Text
$passwordTextBox.Text

然后,当单击按钮时,.bat 文件需要以用户\密码运行。下面的代码更像是伪代码,因为此时我不确定如何执行此操作。我在网上和野生动物园书籍上看过,但我找不到一个例子。我确实找到了一个,但它做了一些不同的事情,我不明白。

$StartButton.Add_Click({Start-Process 
runas $userTextBox.Text\$passwordTextBox.Textc:\temp\Scripts\MapCopyTuner.bat
})

任何帮助都非常感谢,因为你可以说我在这里很环保。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您需要将用户名\密码转换为 PSCredential,并将其传递给 Start-Process

    这是一个示例 powershell sn-p(如果您愿意,可以通过内联变量来减少冗长)。

    $password= convertto-securestring $passwordTextBox.Text -asplaintext –force
    $credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $userTextBox.Text,$password
    $script = "c:\temp\Scripts\MapCopyTuner.bat"
    Start-Process powershell -Credential $credential -ArgumentList "-noprofile -command &{Start-Process $script -verb runas}"
    

    【讨论】:

    • 谢谢 Srikanth.. 你在哪里找到这些信息的?我到处都找遍了..我正在努力寻找更好的资源,这样我就不必烦死你们了...大声笑任何想法的书籍?
    • Powershell 应该为您提供 90% 的答案,get-help Start-Process 在这种情况下会向您显示 -credential 开关,从那里您可以使用 PowerGui 获取一些定义(Nice IDE ) 或在 PS 中使用 get-help Start-Process -Full 继续,Credential 的拼写很好。
    • 类似讨论 here 和来自 MS here
    • @mikey - Scripting GuyPowershell.com 是我经常访问的两个网站。 Powershell in Action 是一本好书。
    • 在您的示例中,-ArgumentList 使用单引号。 $script 因此不会展开。
    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多