powershellpowershell-2.0vmwarepowershell-1.0vmware-server

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


Solution

  • 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:

    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('~');