从 Powershell 以管理员身份运行 .bat 文件 [英] Running a .bat File as Admin from Powershell

查看:86
本文介绍了从 Powershell 以管理员身份运行 .bat 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have been spinning my wheels on this one. I'm creating a small application in powershell gui that needs to run a .bat by passing credentials inputted from a textbox. What I have been trying to figure out is how pass these credentials when a button is clicked on the GUI.

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

I have two boxes on the GUI where the user passes there credentials.

$userTextBox.Text
$passwordTextBox.Text

然后当单击按钮时,.bat 文件需要以用户\密码运行.下面的这更像是伪代码,因为我现在不知道如何做到这一点.我在网上和 safari 书籍上看过,但找不到例子.我确实找到了一个,但它在做一些不同的事情,我不明白.

Then when the button is clicked the .bat file needs to runas user\password. This below is more like psuedo code because I'm not sure how to do this at this point. I have looked online and on safari books but I can not find an example. I did find one but it was doing something different and I did not understand it.

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

非常感谢您的帮助,因为您可以看出我在这里很环保.

Any help is much appreciate, as you can tell I'm very green here.

推荐答案

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

You would need to convert the username\password as PSCredential, and pass it to Start-Process

这是一个示例 powershell 代码段(如果您愿意,可以通过内联变量来简化此操作).

Here is a sample powershell snippet (you can make this less verbose this by inlining variables if you wish).

$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}"

这篇关于从 Powershell 以管理员身份运行 .bat 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆