0

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.

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

$userTextBox.Text $passwordTextBox.Text 

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.

1 Answer 1

7

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

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}" 
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Srikanth.. Where did you find this information? I have looked everywhere.. I'm trying to find better resources so I don't have to bug the hell out of you guys...lol Any Idea's for books?
Powershell should have 90% of the answers for you, get-help Start-Process in this case shows you the -credential switch, from there you can use PowerGui to get some of the definitions (Nice IDE) or continue in PS with get-help Start-Process -Full, and Credential is spelled out pretty well.
Similar discussion here and from MS here
@mikey - Scripting Guy and Powershell.com are two sites that I end up visiting a lot. Powershell in Action is a good book.
In your example -ArgumentList uses single quotes. $script therefore will not expand.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.