PowerShell – Using Sendkeys and Runas.exe

email me

In a local admin profile—while the user is logged in as the local admin—I’m using this to pass a password into runas.exe, to automate a specific process. Make sure you compile this bit of code before using it in production.

Encoding currently leverages PowerShell’s char statement to cast a value to char-array.

Example:

$p1 = [char[]](65)
$p2 = [char[]](71)
$p3 = [char[]](68)

* I will be researching a more secure method of using runas.exe.

$wshell = New-Object -ComObject wscript.shell
$EncodedPW = "$p1$p7$p3$p2$p6$p4$p2$p1$p6$p7"

cmd /c ("start """" runas /u:administrator /netonly ""foo.exe"" ")

$wshell.AppActivate('MyAppWindow Title')
Sleep 1
$wshell.SendKeys('$EncodedPW')
$wshell.SendKeys('{ENTER}')

 

Notes

PowerShell Data Types