Wednesday, 3 April 2019

telnet - SendKeys Method in Powershell


I have batch file for telnet a server automatically, I want to do the same thing with PowerShell


Batch File named Script.bat :


:: Open a Telnet window
start telnet.exe 10.84.10.85
:: Run the script
cscript SendKeys.vbs

Command File named SendKeys.vbs :


set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 1000
OBJECT.SendKeys "myPassword{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "7{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "1{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "{ENTER}"
WScript.sleep 1000
OBJECT.SendKeys "Y{ENTER}"
WScript.sleep 3000
OBJECT.SendKeys ""

Answer



PowerShell has no built-in functionality to emulate keystrokes.


Practically, you have two options: COM-Automation and Interop.



  1. SendKeys via COM


Like in VB(S) you can create a Shell-Object and SendKeys. Here is the PowerShell way to do it.


$wshell = New-Object -ComObject wscript.shell;
$wshell.SendKeys('a')

If you would like to send a keystroke to a window, you have to activate it first:


$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('title of the application window')
Sleep 1
$wshell.SendKeys('~')

Some keystrokes have special variables like ~ for RETURN. Here is a complete list.
After activating a window it's often necessary to wait a second until it becomes responsive, otherwise it'll send the key to the PowerShell window, or to nowhere. The scripting Host's SendKeys method can be unreliable, but luckily there is a better approach.



  1. SendKeys via Interop


Like in C#, you can use the SendWait method from the .NET Framework in PowerShell.


[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("x")

If you want to activate a window, it can be done like this:


[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
[Microsoft.VisualBasic.Interaction]::AppActivate("Internet Explorer - Windows")

To Sleep, you can use the Start-Sleep Cmdlet.


Regarding your original problem, I would suggest the following solution:


# Open a Telnet window
Start-Process telnet.exe -ArgumentList 10.84.10.85
# Run the keystrokes
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('myPassword{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('7{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('1{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('Y{ENTER}')
Start-Sleep 1
[System.Windows.Forms.SendKeys]::SendWait('')

WARNING: Be extra careful if you're using this method to send a password because activating a different window between invoking AppActivate and invoking SendKeys will cause the password to be sent to that different window in plain text (e.g. your favorite messenger)!


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...