简体   繁体   中英

powershell Invoke-Expression with sendkey

Is there a way in which is can pass a parameter through SendKey in Powershell to my custom application

Something like EXPECT in TCL

#!/usr/bin/perl -w
use Expect;
my $exp = new Expect;

my $command = 'ssh -l user 10.10.10.25';

$exp->spawn($command) or die "Cannot spawn $command: $!\n";

my $patidx = $exp->expect(30, 'Enter passphrase for key');
$exp->send("password\n");

I tired using the following but it doesnt seem to invoke my custom application

$a = Get-Process | Where-Object {$_.Name -eq "Notepad"}
wait
[Microsoft.VisualBasic.Interaction]::AppActivate($a.ID)
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
wait
[System.Windows.Forms.SendKeys]::SendWait("{F5}{ENTER}")

my customer application would be like C:\\somecli -option1 -option2 -option 3

I have a cli command say ping -t 10.10.10.1 here the command is "ping" and parameters are "-t 10.10.10.1" , so when i initially invoke the command i am using "ping -t 10.10.10.1" hypothetically suppose i need to pass another parameter say: y to this command after some execution has been done, sendkey does allow i

You can do it with WASp shap in http://wasp.codeplex.com/

Here's example from their site:

Select-Window notepad | Send-Keys "%(ea)Testing{Enter}{F5}"

I don't quite understand what you mean by 'passing parameters' to SendKeys, but SendKeys will only work on the currently focused window, meaning you can't input keys to any application running in the background. The previously mentioned WASP module for PowerShell just uses SendKeys internally, so if WASP won't work, then neither will typing out the SendKeys functions.

As far as 'will this work for any custom application', SendKeys is based on the WinForms API, so it will only be able to interact with applications that use that API.

Edit:

Based on your edit, it seems like you just want to send keys that are defined at runtime. Just assign your keys to a variable (like $keys="Keys I want to send" ), and then just send the variable (like [System.Windows.Forms.SendKeys]::SendWait($keys) ). You can use Read-Host if you want the user to type in the keys to send.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM