49

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

HamTheAstroChimp
  • 1,275
  • 4
  • 15
  • 28
  • 1
    Which is the script i wrote, requires the **ENTER** keystroke. instead of hitting **enter** button all the time, simply i want to automate. – HamTheAstroChimp Jul 25 '13 at 06:16
  • Why does it require pressing enter? Are you executing read-host? – Keith Hill Jul 25 '13 at 06:31
  • Myscript continuosly login/logout from several machine. By the time of logout it requires confirmation, so i have to hit **enter** button. This is the reason i want automate. – HamTheAstroChimp Jul 25 '13 at 07:12

5 Answers5

75

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('~');
mklement0
  • 382,024
  • 64
  • 607
  • 775
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
  • 3
    Thanks for that Add-Type addition to the end there. Was banging my head against the wall for an hour trying to get my script working. – sliderhouserules Mar 23 '15 at 20:04
  • What are the differences between using `Wscript.Shell` and using `SendKeys.SendWait`? Is one better that the other? – tfpf Jul 22 '21 at 20:29
3
function Do-SendKeys {
    param (
        $SENDKEYS,
        $WINDOWTITLE
    )
    $wshell = New-Object -ComObject wscript.shell;
    IF ($WINDOWTITLE) {$wshell.AppActivate($WINDOWTITLE)}
    Sleep 1
    IF ($SENDKEYS) {$wshell.SendKeys($SENDKEYS)}
}
Do-SendKeys -WINDOWTITLE Print -SENDKEYS '{TAB}{TAB}'
Do-SendKeys -WINDOWTITLE Print
Do-SendKeys -SENDKEYS '%{f4}'
FreeSoftwareServers
  • 2,271
  • 1
  • 33
  • 57
1

Send "Enter" key to an App, for example for pressing "OK". Works great:

Add-Type -AssemblyName microsoft.VisualBasic

Add-Type -AssemblyName System.Windows.Forms

# Get the desired process:

$ProcessName = Get-Process -Name Calculator

Start-Sleep -Seconds 1

# If the process is actually running, bring it to front:

If ($ProcessName)
{
   (New-Object -ComObject.Wscript.Shell).AppActivate((Get-Process $ProcessName -ErrorAction SilentlyContinue).MainWindowTitle)
}

# Send "Enter" key to the app:

[Microsoft.VisualBasic.Interaction]::AppActivate($ProcessName.ProcessName)
[System.Windows.Forms.SendKeys]::SendWait({'~'})
Maor Zohar
  • 592
  • 5
  • 17
0

Also the $wshell = New-Object -ComObject wscript.shell; helped a script that was running in the background, it worked fine with just but adding $wshell. fixed it from running as background! [Microsoft.VisualBasic.Interaction]::AppActivate("App Name")

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
0

For remote desktop connection after user name and password authentication it is asking for warning message per company policy, the enter command is not accepting to it, how to give ok to it

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 14 '23 at 20:40
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/34384255) – Keith Langmead May 18 '23 at 19:56