Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform keystroke inside powershell?

I have ps1 script to grab some information from the vmware cluster environment.

In some place of ps1 script requires the ENTER button keystroke.

So, How to do that ?

-Thanks

like image 638
HamTheAstroChimp Avatar asked Jul 25 '13 05:07

HamTheAstroChimp


People also ask

How do I simulate keystrokes in PowerShell?

Use the SendKeys Method to Perform Keystrokes Inside PowerShell. You can use the SendKeys() method to send keystrokes to the active application. The AppActivate() method activates the application Notepad so you can send keystrokes to it.

What is Ctrl break in PowerShell?

In the PowerShell console you use the Ctrl+Break key combination to break into the debugger.

How do I scroll using the keyboard in PowerShell?

In Windows 10 Powershell use Ctrl + PgUp / PgDn to scroll by line. In Windows 10 cmd use Ctrl + ↓ / ↑ to scroll by line.

How do I capture a keystroke in PowerShell?

To capture a key press in PowerShell, execute the following command: The command above captures a keystroke and records the information about the pressed key to the $PressedKey variable. To print the information about the captured keystroke, execute:

How to send keystrokes from the console to the active application?

PowerShell is a powerful scripting language that can manage and automate different tasks in the system. It lets you send keystrokes from the console to the running application. You can use the SendKeys () method to send keystrokes to the active application.

Can I use VBScript sendkeys with PowerShell?

The second option is a codeplex project named WASP (Windows Automation Snapin for PowerShell). There is nothing wrong with using the VBScript SendKeys function inside Windows PowerShell.

Is it possible to implement keyboard input intercept in PowerShell?

Therefore, it might be possible that implementations in PowerShell appear. The action carrying out this function in our PowerShell is the installation of a hook in order to monitor and intercept keyboard input events before these arrive to the applications.


1 Answers

If I understand correctly, you want PowerShell to send the ENTER keystroke to some interactive application?

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

If that interactive application is a PowerShell script, just use whatever is in the title bar of the PowerShell window as the argument to AppActivate (by default, the path to powershell.exe). To avoid ambiguity, you can have your script retitle its own window by using the title 'new window title' command.

A few notes:

  • The tilde (~) represents the ENTER keystroke. You can also use {ENTER}, though they're not identical - that's the keypad's ENTER key. A complete list is available here: http://msdn.microsoft.com/en-us/library/office/aa202943%28v=office.10%29.aspx.
  • The reason for the Sleep 1 statement is to wait 1 second because it takes a moment for the window to activate, and if you invoke SendKeys immediately, it'll send the keys to the PowerShell window, or to nowhere.
  • Be aware that this can be tripped up, if you type anything or click the mouse during the second that it's waiting, preventing to window you activate with AppActivate from being active. You can experiment with reducing the amount of time to find the minimum that's reliably sufficient on your system (Sleep accepts decimals, so you could try .5 for half a second). I find that on my 2.6 GHz Core i7 Win7 laptop, anything less than .8 seconds has a significant failure rate. I use 1 second to be safe.
  • IMPORTANT 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!

Sometimes wscript.shell's SendKeys method can be a little quirky, so if you run into problems, replace the fourth line above with this:

Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.SendKeys]::SendWait('~'); 
like image 87
Adi Inbar Avatar answered Sep 19 '22 19:09

Adi Inbar