Handling a popup box using powershell

穿精又带淫゛_ 提交于 2019-12-02 09:53:32
Nate Hekman

With Powershell v2 you can use PInvoke to access the normal Win32 API, thus giving you access to FindWindow and SetForegroundWindow. Then use SendKeys to send a Enter key.

Something like this to register the methods:

$pinvokes = @'
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern IntPtr FindWindow(string className, string windowName);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
'@

Add-Type -AssemblyName System.Windows.Forms
Add-Type -MemberDefinition $pinvokes -Name NativeMethods -Namespace MyUtils

Now you can find the window you need:

$hwnd = [MyUtils.NativeMethods]::FindWindow(null, "Popupboxname")

Give it focus:

[MyUtils.NativeMethods]::SetForegroundWindow($hwnd)

And send the Enter key:

[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

Sources/inspiration:

You might want to investige the WASP snapin from CodePlex:

http://wasp.codeplex.com/wikipage?title=Some%20Usage%20Examples&referringTitle=Home

user11898394

Try something like this:

$wshell = New-Object -ComObject wscript.shell; 
$wshell.AppActivate('Vector RP1210 API Setup') 
Sleep 1 
$wshell.SendKeys('%C')
$wshell.AppActivate('Vector RP1210 API') 
Sleep 1 
$wshell.SendKeys('{ENTER}')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!