Monday, February 21, 2011

PowerShell and interactive external programs

Hi all

I'm attempting to write a PowerShell script that, among other things, runs two external programs, harvesting the output of one and providing it to the other.

The problem is that the second program is interactive and asks for:
- a password
- an option (1, 2, or 3)
- an option (Y or N)
- output of external program 1

Note also that this is on XP with PowerShell v1 and .net v2.0 (no I can't upgrade)

Any ideas how I would do this?

CC

From stackoverflow
  • Try to see if you can pass the options in via stdin e.g.:

    $prog1out = prog1
    "mypassword`r`n1`r`nY`r`n$prog1out" | prog2
    

    If that doesn't work, another way (kind of hackish and perhaps brittle) is to use SendKeys e.g.:

    [reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
    [windows.forms.sendkeys]::SendWait("mypassword`r`n")
    Start-Sleep -sec 1 # may need to vary this
    [windows.forms.sendkeys]::SendWait("1`r`n")
    # rinse and repeat
    

    Note that SendKeys works against the foreground window which might be a problem for you unless you can make sure the PowerShell prompt remains the foreground window.

0 comments:

Post a Comment