Handling a popup box using powershell

前端 未结 3 1800
再見小時候
再見小時候 2021-01-28 21:43

Can anyone please tell me how to click on \"ok\" or \"cancel\" on a popup window using powershell? I am trying to automate a website using powershell, but I am new to powershell

相关标签:
3条回答
  • 2021-01-28 22:20

    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}')
    
    0 讨论(0)
  • 2021-01-28 22:25

    You might want to investige the WASP snapin from CodePlex:

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

    0 讨论(0)
  • 2021-01-28 22:33

    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:

    • power shell : how to send middle mouse click?
    • http://poshcode.org/1837
    • http://www.tek-tips.com/viewthread.cfm?qid=1563429
    0 讨论(0)
提交回复
热议问题