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.

Mikey
  • 133
  • 1
  • 4
  • 17

1 Answers1

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}"
Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76
  • 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? – Mikey Mar 09 '13 at 21:11
  • 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. – Austin T French Mar 09 '13 at 23:47
  • 1
    Similar discussion [here](http://stackoverflow.com/questions/6239647/using-powershell-credentials-without-being-prompted-for-a-password) and from MS [here](http://social.technet.microsoft.com/wiki/contents/articles/4546.working-with-passwords-secure-strings-and-credentials-in-windows-powershell-en-us.aspx) – noam Mar 10 '13 at 00:58
  • @mikey - [Scripting Guy](http://blogs.technet.com/b/heyscriptingguy/) and [Powershell.com](http://powershell.com/cs/) are two sites that I end up visiting a lot. [Powershell in Action](http://www.amazon.com/dp/1932394907/) is a good book. – Srikanth Venugopalan Mar 10 '13 at 01:33
  • @Mikey - you are welcome, please mark this as answer if you are happy with the solution. – Srikanth Venugopalan Mar 11 '13 at 07:58
  • I found this to get username/password for credentials: function switch-user { param($Process="powershell.exe") $cred = Get-Credential Start-Process $Process -Credential $cred -ArgumentList [yourstuffhere] } You paste the function at the top of powershell script or session in this example. This function pops open a login window for username/password. – JustJohn Feb 09 '17 at 19:05
  • 1
    In your example `-ArgumentList` uses single quotes. `$script` therefore will not expand. – Matt Feb 11 '17 at 02:44