Jump to content
Sign in to follow this  
Graywalker

Get URL from Broswer (all tabs) using PID - remotely?

Recommended Posts

Graywalker

Task : If Java.exe opens and it's parent process is a browser, get the URL of the web page it has open (hopefully the URL that started java).

I would love a way to do it remotely, but can live with having to run a monitor on the local machine.

What I have now is a function that uses WMI to query win32_process and get the processes and parent process id, searches that for Java and puts that info into a report.

I want to include the URL of the webpage that launched java. What I have so far is below, but it doesn't let me tie the Process ID to the window I get the URL from. It also will not get Firefox windows. The Shell method only gets Shell windows. I have nine windows open, but it only picks up two - internet explorer and file system explorer - missing both Firefox windows and all their tabs.

So, the question is - how to get a browser object when I start with only a Process ID and get the URL from that object? On a remote computer?

$oShell = ObjCreate("shell.application") ; Get the windows shell object

$oShellWindows = $oshell.windows ; Get open windows

If IsObj($oShellWindows) Then
    $string = ""
    For $Window in $oShellWindows ; count all windows
        $Wpid = ObjName($Window,3)
        $string = $string & $Window.LocationName & " ; " & $Window.FullName & " ; " & $Wpid & @CRLF
        If StringInStr($Window.FullName, "iexplore") Then
            MsgBox(0,"Internet Explorer", "You browsing the URL " & $Window.Document.Location.href)
        EndIf

    Next
    MsgBox(0,"Shell Windows", "You have the following " & $oShellWindows.Count & " windows open:" & @CRLF & $string);
EndIf

Fantasy code I would like to work:

$objWMIService = ObjGet("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & $strPCName & "\root\cimv2")
If IsObj($objWMIService) Then
  $colProcesses = $objWMIService.ExecQuery( _
    "select * from win32_process")
For $objProcess In $colProcesses
If $objProcess.ProcessId = $javaparentPID Then
   $Name = $objProcess.LocationName
   $URL = $objProcess.Document.Location.href
Endif

Code that gets the process IDs :

Func _ProcessInfo($strPCName)
Dim $i = 0
Dim $User, $Domain, $objWMIService, $colProcesses, $procnum
Dim $Aprocesses[1][9]
Dim $pi, $ppi, $parent, $parentfound
$objWMIService = ObjGet("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & $strPCName & "\root\cimv2")
; Gather process information into an array
If IsObj($objWMIService) Then
  $colProcesses = $objWMIService.ExecQuery( _
    "select * from win32_process")
  For $objProcess In $colProcesses
   $i = $i + 1
   ReDim $Aprocesses[$i + 1][9]
   $Aprocesses[$i][1] = $objProcess.ProcessId
   $Aprocesses[$i][2] = $objProcess.Caption
   $Aprocesses[$i][3] = $objProcess.HandleCount
   $Aprocesses[$i][4] = $objProcess.ExecutablePath
   $Aprocesses[$i][5] = $objProcess.ParentProcessId
   $Aprocesses[$i][6] = $objProcess.CreationDate
   If $objProcess.GetOwner($User, $Domain) = 0 Then
    $Aprocesses[$i][7] = $Domain & _
      "\" & $User
   Else
    $Aprocesses[$i][7] = ""
   EndIf
  Next
  $Aprocesses[0][0] = $i
  $procnum = $i
  ;_ArrayDisplay($Aprocesses)
  ; Process the information about the Processes - LOL!
  $parentfound = 0
  For $pi = 1 To $procnum
   ; only looking for Java, so :
   If StringInStr($Aprocesses[$pi][2], "java") Then
    $parent = $Aprocesses[$pi][5]
    For $ppi = 1 To $procnum
     If $Aprocesses[$ppi][1] = $parent Then
      $parentfound = 1
      ExitLoop
     EndIf
    Next
    If $parentfound = 1 Then
     ; It has a parent process still running!
     $line = $strPCName & "," & $Aprocesses[$pi][1] & "," & $Aprocesses[$pi][2] & "," & $Aprocesses[$pi][3] & "," & _
       $Aprocesses[$pi][4] & "," & $Aprocesses[$pi][5] & "," & $Aprocesses[$pi][6] & "," & _
       $Aprocesses[$ppi][6] & "," & $Aprocesses[$ppi][2] & "," & $Aprocesses[$ppi][4] & "," & _
       $Aprocesses[$ppi][7]
     FileWriteLine($LogFile, $line)
    Else
     ; parent process left. No, little java, its not your fault... :)
     $line = $strPCName & "," & $Aprocesses[$pi][1] & "," & $Aprocesses[$pi][2] & "," & $Aprocesses[$pi][3] & "," & $Aprocesses[$pi][4] & "," & _
       $Aprocesses[$pi][5] & "," & $Aprocesses[$pi][6] & ",Parent already closed or not found" & ",Owner = ," & $Aprocesses[$pi][7]
     FileWriteLine($LogFile, $line)
    EndIf
   EndIf
   $parentfound = 0
   $ppi = 0
  Next
Else
  FileWriteLine($LogFile, $strPCName & ",Unable to query WMI on endpoint")
EndIf
EndFunc   ;==>_ProcessInfo
Edited by Graywalker

Share this post


Link to post
Share on other sites
Graywalker

MsgBox(0,"Internet Explorer", "You browsing the URL " & $Window.LocationURL)

works just as well as

MsgBox(0,"Internet Explorer", "You browsing the URL " & $Window.Document.Location.href)

Something odd - I am on Windows 7 64bit - I have an open IE window.

$oIE = ObjGet("","InternetExplorer.Application")
$url = $oIE.LocationURL
$name = $oIE.LocationName

Returns an error on $url = $oIE.LocationURL of "==> Variable must be of type "Object".:"

Edited by Graywalker

Share this post


Link to post
Share on other sites
Graywalker

Wow... I guess it is complicated... lol!

So, I've discovered a few ways to get the URL from the Window based on the window title :

AutoItSetOption("WinTitleMatchMode", 2)
If WinExists("Internet Explorer") Then
$wtext = WinGetText("Internet Explorer")
$wtexta = StringSplit($wtext, @LF)
For $l = 1 To $wtexta[0]
  If StringInStr($wtexta[$l], "http") Then
   $url = $wtexta[$l]
   ExitLoop
  EndIf
Next
EndIf
MsgBox(0, "URL", $url)
Endif

OR

AutoItSetOption("WinTitleMatchMode", 2)
#include <IE.au3>
$awins = WinList("Internet Explorer")
For $w = 1 to $awins[0][0]
  $oBrowser = _IEAttach($awins[$w][1],"HWND")
  If IsObj($oBrowser) Then
  $bURL = $oBrowser.LocationURL
  $bName = $oBrowser.LocationName
  MsgBox(0,"Browser Info",$bName & @CRLF & $bURL)
  EndIf
Next

But neither lets me a) Get it remotely :) Tie it directly to the Process ID - and both rely on the Window Title having "Internet Explorer" in it. (edit to clarify - that means that if there are multiple windows of Internet Explorer open, I can't be sure which URL is the one that opened Java.)

Not happy.... any ideas?

... and why THIS doesn't work, I don't know!!

$oBrowser = _IEAttach($objProcess.Handle, "HWND")
If IsObj($oBrowser) Then
  $bURL = $oBrowser.LocationURL
  $bName = $oBrowser.LocationName
  MsgBox(0, "Browser Info", $bName & @CRLF & $bURL)
Else
  MsgBox(0, "NOT an Object!", "Dummy!!")
EndIf

... actually, I suppose I do... I think the "Handle" returned by Win32_Process is actually the Process ID, not a "HWND" handle.

Edited by Graywalker

Share this post


Link to post
Share on other sites
Graywalker

More info! Getting closer....

The following code gets the PID with the window - because once you have the handle, you can use the handle instead of the window title.

However, with tabbed browsing, the WinGetText($handle) only gets the tab that is in focus. Since most of my enterprise is on IE 7, this won't be much of an issue... BUT... it could get that way. Also, _IEAttach seems to have the same issue with tabs.

Any ideas how to get the URLs of tabs?

If StringInStr($Aprocesses[$i][4], "iexplore") Then
   $Pid = ""
   $wtext = ""
   $url = ""
   If WinExists("Internet Explorer") Then
    $awins = WinList("Internet Explorer")
    ;_ArrayDisplay($awins)
    For $w = 1 To $awins[0][0]
     $Pid = WinGetProcess($awins[$w][1])
     ;If $Pid = $Aprocesses[$i][1] Then
      $wtext = WinGetText($awins[$w][1])
      $wtexta = StringSplit($wtext, @LF)
      For $l = 1 To $wtexta[0]
       If StringInStr($wtexta[$l], "http") Then
        $url = $wtexta[$l]
        ExitLoop
       EndIf
      Next
     ;EndIf
     MsgBox(0,"Array Info",$Pid & @CRLF & $url)
    Next
   EndIf

Share this post


Link to post
Share on other sites
Graywalker

Got the URLs of the Tabs!

The following code seems to be mostly working - on a local machine. It doesn't work with Firefox, but that is not absolutely necessary at this point, although I would LOVE it if someone provided a solution to that - and doing it from a central computer to other computers on the local network.

The code lists all windows with "Internet Explorer" in the title bar, then tries to get an object from each - if it is not an object on it's own, it tries to see if it is embedded in the previous object. Either way, it tries to get the Title and the URL of the window - hopefully Internet Explorer. Unfortunatly, if someone changed the IE window title to "Internut Exploder", then this won't work too well. :)

AutoItSetOption("WinTitleMatchMode", 2)
AutoItSetOption("WinDetectHiddenText", 1)

$awins = WinList("Internet Explorer")
$mainhwnd = ""
For $w = 1 To $awins[0][0]
  FileWriteLine($logfile, "Windows array " & $w & "," & $awins[$w][0] & "," & $awins[$w][1])
  $oBrowser = _IEAttach($awins[$w][1], "HWND")
  If IsObj($oBrowser) Then
   $oldmainhwnd = $mainhwnd
   $mainhwnd = $awins[$w][1]
   $bURL = _IEPropertyGet($oBrowser, "locationurl");$oBrowser.Document.Location.href
   $bName = _IEPropertyGet($oBrowser, "title");$oBrowser.Location
   $Pid = WinGetProcess($awins[$w][1])
   FileWriteLine($logfile, $bName & "," & $bURL & "," & $Pid)
  Else
   If $oldmainhwnd = $mainhwnd Then
    ;Do Nothing, same old stuff.
   Else
    For $w2 = 1 To $awins[0][0]
     $oBrowser = _IEAttach($mainhwnd, "Embedded", $w2)
     If IsObj($oBrowser) Then
      $bURL = _IEPropertyGet($oBrowser, "locationurl");$oBrowser.Document.Location.href
      $bName = _IEPropertyGet($oBrowser, "title");$oBrowser.Location
      $Pid = WinGetProcess($awins[$w][1])
      FileWriteLine($logfile, $bName & "," & $bURL & "," & $Pid & ", Using HWND " & $mainhwnd & ", $w2 = " & $w2 & ", old hwnd = " & $oldmainhwnd)
     Else
      ; Do nothing - maybe exitloop??
     EndIf
    Next
    $oldmainhwnd = $mainhwnd
   EndIf
  EndIf
Next

If anyone shows interest, I will post the whole script to look for java and get the URLs that called it when I get that script completed. This piece was a monster all by itself.

Edited by Graywalker

Share this post


Link to post
Share on other sites
Graywalker

Nothing? Really?

Okay, how about a way to get the IE History remotely using AutoIT? Maybe the last 10 URLs visited? I hope that would include the currently open windows....

The script I wrote is not going to work, because of the way the User environments work... I can get the running processes of a user, but I can't, with an admin account, see the currently open windows on the user's account using _IEwhatever UDF or the WinList function. (running script using scheduled task as admin)

So... how to get the URLs???

Edited by Graywalker

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • tommytx
      By tommytx
      ConsoleWrite("Hello Dolly on Line 1" & @CRLF) #include <IE.au3> ConsoleWrite("Hello Dolly on Line 5" & @CRLF) Local $oIE = _IECreate("www.autoitscript.com") ConsoleWrite("Hello Dolly on Line 10" & @CRLF) Can anyone put me on the right track to troubleshoot this one.. the IE browser is not opening.. it is not my primary browser since FF is but that has not been a problem in the past.. it all seems to work including compile but the generation of the IE browser.. Never had any problem before.. below is the results of the run.. any ideas on where I might look.. that is the only error i get and its only after a 30 second delay.. I suppose a time out..
       
      ; >"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "G:\show_pos.au3\test.au3"
      ; Hello Dolly on Line 1
      ; Hello Dolly on Line 5
      ; --> IE.au3 T3.0-2 Error from function _IECreate,  (Browser Object Creation Failed)
      ; Hello Dolly on Line 10
      ; >Exit code: 0    Time: 30.46
       
    • ohaya
      By ohaya
      Hi,
      I am still really new with AutoIT.  We are using it to automate logging into web sites and I have encountered problems with focus.  The target web page is configured to put the cursor into the first text field (username) when the page is loaded, and when I run the AutoIT script, which does the log in seems like it is just not starting where I expect it to be.  
      I have been kind of using ToolTip() to kind of help with debugging, but now I am wondering if the calls to ToolTip() are causing the focus to be messed up.  
      For example, at least visually, when the ToolTip() is called, I can see the cursor disappear from the web page text field and they when I do anything that is supposed to send keystrokes, they are going off somwhere else ("never-neverland").
      But when I remove some of the ToolTip() calls, it works correctly.
      So the questions I have are:
      1) Do the ToolTip() calls interfere with/change where the focus on the target page are?
      2) In general, what are the "rules" for where ToolTip can be used "safely" (== doesn't interfere with focus)?
      Thanks,
      Jim
       
    • Rocker
      By Rocker
      I apologize for my poor coding skills, but only on occasion do I have a need to automate a repetitious task to save time.  In a nutshell the following code is supposed to go to a website, save the site using Scrapbook (a plugin used by an older version of Firefox), rename the folder in Windows Explorer that Scrapbook assigns a random name for and then moves on to the next website, of which there are hundreds.  Everything works, except after the address bar gets populated with the URL, it's like the ENTER key never gets pushed.  The weird thing is, this worked yesterday before I left to go home and when I came in this morning it didn't work.  The next thing I'll be trying to learn is to loop this with a the list of the websites.  Thank you for your help!
      WinActivate( "[CLASS:MozillaWindowClass]" ) ;Apply focus to the open Mozilla firefox window Sleep(500) Send("^k") ;Apply focus to the address bar Sleep(500) Send("www.WEBSITE.com {ENTER}") ;Send the website to the address bar Sleep(10000) ;Wait for the website to fully open Send("!c") ;Opens the drop down menu for Scrapbook Sleep(2000) Send("{ENTER}") ;Selects the default drop down selection of save Sleep(20000) WinActivate( "[CLASS:CabinetWClass]" ) ;Applies focus to the open Windows Explorer window where the websites are saved Sleep(1000) Send("{SPACE}") ; Selects a folder in the folders list Sleep(200) Send("{HOME}") ;Moves to the top folder Sleep(200) Send("{F2}") ;Keyboard shortcut to rename the folder Sleep(200) Send("www.WEBSITE.com {ENTER}") ;Applies the new name to the folder  
    • nacerbaaziz
      By nacerbaaziz
      Hello all
      I have a question please
      I have a link to an .exe file
      for example this link
      https://www.nvaccess.org/files/nvda/releases/2017.1/NVDA_2017.1.exe
      I want to check if the link is exists orno
      surely  Without downloading the file
      is there any  function or a way to  do this?
      Thanks in advance
    • BlazerV60
      By BlazerV60
      Hello all,
      I've written the code below which launches chrome in incognito mode and then proceeds to go to the autoit website.
      From my understanding, the Run() command is also supposed to output the PID number related to the application that got launched from the Run command.
      However when I run the below lines, it outputs a PID number that is different from the newly launched chrome browser's PID number, does anyone know why and possibly explain how I could retrieve the accurate PID number associated with the newly launched browser?
      Global $iPid = Run(@ComSpec & ' /c start chrome.exe https://www.autoitscript.com/forum/ -incognito' ,"", "") msgbox(0,"",$iPid) Thank you,
      Brian
×