Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to handle a browser window that opens up during the execution of some external program commands

by tarunmudgal4u (Sexton)
on Mar 28, 2013 at 11:54 UTC ( #1025947=perlquestion: print w/ replies, xml ) Need Help??
tarunmudgal4u has asked for the wisdom of the Perl Monks concerning the following question:

I'm running a script that invokes a powershell prompt and fires some commands. During this process, the powershell program triggers a browser window where a user needs to enter his credentials and press the login button. Once it's done, user sees the welcome screen and enter a verification button. Now, rest of the script runs.

I don't understand how to handle this browser window during the execution of this Perl script. The browser window that opens up doesn't display the url as well.

Below is the code that I'm running-

unlink "powershell.out" if (-e ".\powershell.out"); $input = "date"; $program = "powershell.exe -STA -ExecutionPolicy RemoteSigned -command + - >> powershell.out"; $pid = open(WRITEME, "| $program") or die "Couldn't fork: $!\n"; print WRITEME "date\n"; print WRITEME ". ./PowerShell/common/Get-SepmRmmWsAccessToken.ps1 -sym +-383925ff28c -port 8446\n"; print WRITEME "Get-SepmRmmWsAccessToken -HostName 10.211.34.102 -Port +8446 -ClientId 7cbce559-2eca-4ff8-806b-b2e511a239d0 -ClientSecret 729 +05a1c-2953-4607-bb9a-0e92060308b7"; close(WRITEME) or die "Couldn't close: $!\n"; waitpid($pid, 1);

Please let me know your valuable inputs.

Comment on How to handle a browser window that opens up during the execution of some external program commands
Download Code
Re: How to handle a browser window that opens up during the execution of some external program commands
by ggoebel (Sexton) on Mar 28, 2013 at 12:15 UTC

    Try using Authen::NTLM to authenticate to that domain before executing your powershell script.

Re: How to handle a browser window that opens up during the execution of some external program commands
by McA (Curate) on Mar 28, 2013 at 13:27 UTC

      Hi McA, Thanks for your help in previous post :) As you can see in this script snippet, I redirected the powershell STDOUT to a file. It's not the exact solution of my earlier problem but, it's just a workaround.

      In my earlier post I wanted to store the powershell script output in a variable. It didn't work using open2 function. So, to move ahead with my objective in a faster way, I used this workaround. Now, here again as you see, I found a trouble :) This time my powershell script invoked a browser, where I need to put my credential and click on the validation button.

      Here, I think I can solve this problem in two ways, either modify the powershell code itself or find a way in perl to handle it. First way to modify powershell script seems reluctant to me as it would require powershell knowledge and also modification in the existing scripts (this thing I want to avoid). Second way is good as it would enhance my Perl skills and I would love that :)

Re: How to handle a browser window that opens up during the execution of some external program commands
by hdb (Prior) on Mar 28, 2013 at 13:35 UTC

    I am on Internet Explorer and found Win32::IEAutomation useful in the past as it emulates the user interaction with the browser. Have a look at the code snippet below. This is a bit older code and I hope it still works on newer IE.

    my $ie = Win32::IEAutomation->new( visible => 1 ); $ie->gotoURL( "http://some.site.com/" ); # login $ie->getTextBox('name:', "userid")->SetValue($user); $ie->getTextBox('name:', "password")->SetValue($pass); $ie->getButton('name:', "login.x")->Click; # move to job page $ie->getLink('linktext:', "Job")->Click; $ie->getLink('linktext:', $jobid)->Click; # retrieve results my $content = $ie->PageText;
Re: How to handle a browser window that opens up during the execution of some external program commands
by technojosh (Priest) on Mar 28, 2013 at 15:25 UTC
    Post the Powershell code that launches and manipulates IE, and I'll tell you how to handle the trivial part of filling in fields. The PS code is already using something to automate IE, and... oh wait, this is a Perl forum, not a Powershell forum.

    If you must take control of a window using Perl, on Windows, that was launched by another process (Powershell in this case), then Win32-GuiTest is probably your best bet...

      Nevermind what forum this is ... we all encounter issues like this every day.   Bring the topic to closure here (too...), please.   PowerShell scripts will be understood by all comers.

        I asked the OP to post the powershell code, and offered to help with it

      Hi, I'm trying to use Win32-GuiTest for this problem. Below is my observation while using this module. please let me know your suggestions on this-

      1. My powershell initiated browser window doesn't has any title. So, I'm using FindWindowLike with class regex. It works somehow okay but in some scenarios, this class regex matches more windows. Is there any way to get only one window handler?

      2. Once I get the window handler, is there any function to select username, password text boxes and send texts into these text boxes.

      below is the sample code i'm using to identify

      use Win32::GuiTest qw( :ALL ); #use strict; my @whnds = FindWindowLike( undef, "^\s*\$" , "Internet Explorer_S +erver" ); if( !@whnds ){ die "Cannot find window with title/caption Symantec Endpoint P +rotection Manager\n"; }else{ print "window handle found: [@whnds]\n"; } $text = "hi"; foreach (@whnds) { set_front_win($_); $result= WMSetText( $_, $text ); SendKeys("{TAB}"); # sleep(1); SendKeys("admin"); # sleep(1); SendKeys("{TAB}"); # sleep(1); SendKeys("crt{@}123"); # sleep(1); SendKeys("{TAB}"); # sleep(1); SendKeys("{TAB}"); # sleep(1); SendKeys("{ENTER}"); sleep(1); #PushChildButton( undef, "Authorize" ); SendKeys("{ENTER}"); }

        To answer both questions see spy.pl from Win32::GuiTest::Examples. Use this tool (or something similar) to determin the window/object ID. See the other examples for using this ID to interact with them.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025947]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2014-09-22 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (177 votes), past polls