Use powershell to populate dialog box from another

2019-08-06 04:12发布

问题:

I am writing a script to uninstall and reinstall a piece of software.

Annoyingly, you have to enter a password as part of the uninstall process. (i.e. the uninstaller pops up with a box, the user enters a password to continue)

I want to be able to detect the dialog box, and enter the password automatically - so the user doesn't have to do anything. I seem to remember there's a way to do this, but not sure how.

回答1:

Figured it out:

$app = get-process | where {$_.mainwindowtitle -match "Admin Password"} 

$processID = 0

foreach ($a in $app) {

        if ($a.id -gt $processID)
         {
        $processID = $a.id
    }

}

start-sleep -Milliseconds 500

[Microsoft.VisualBasic.Interaction]::AppActivate($processID)
[System.Windows.Forms.SendKeys]::SendWait("mypassword{ENTER}")

Works great inmy demo app. Trouble is the app I am trying to send the password to is protected from SendKeys.

D'oh. Back to the drawing board...