2

I am working on a script that serves as a support ticket creator using PowerShell. When activated at any random time in the context of any user, the script shall look for open browser tabs in msedge, firefox, and IE, and grab the focused windows' URLs if they are open.

I looked through this forum and many others, found many negative responses and one link talking about a solution but the download link to the script does not work anymore.

That is why I wanted to ask: is there any solution to this? My script already grabs the Window Title properties:

$msedgetitle = Get-Process MSEdge | select -expandproperty MainWindowTitle -ErrorAction SilentlyContinue
$chrometitle = Get-Process chrome | select -expandproperty MainWindowTitle -ErrorAction SilentlyContinue
$iexploretitle = Get-Process iexplore | select -expandproperty MainWindowTitle -ErrorAction SilentlyContinue

Many thanks in advance AÖ

Zilog80
  • 2,534
  • 2
  • 15
  • 20
A. Ö.
  • 21
  • 1
  • 2
  • It seems a bit weird to grab a URL from a browser and then use it in an automated script. I searched for a solution but it's not simple. Maybe you should reconsider on how or what you're doing and how you're trying to achieve it. – DarkLite1 Apr 28 '21 at 13:22
  • Thank you for your reply DarkLite1. The intent is to give IT support an idea of what problem the user is encountering on a current webpage. The script generates an email with some prefilled info, a screenshot and the window title or even better the URL. Is it not simple or impossible from your point of view? – A. Ö. Apr 28 '21 at 13:59

2 Answers2

0

This code will return the url of the active tab in Chrome. Note the timing is crucial and will need to be experimented on using your system.

function Show-Process($Process, [Switch]$Maximize) {

<#
  Function Courtsy of:
  community.idera.com/database-tools/powershell/powertips/b/
  tips/posts/bringing-window-in-the-foreground
#>

  $sig = '
    [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd);
  '
  
  if ($Maximize) { $Mode = 3 } else { $Mode = 4 }
  $type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
  $hwnd = $process.MainWindowHandle
  $null = $type::ShowWindowAsync($hwnd, $Mode)
  $null = $type::SetForegroundWindow($hwnd) 
}

Clear-Host
Set-Clipboard -Value $null
$wshell=New-Object -ComObject wscript.shell
$Null = $wshell.AppActivate('Chrome') # Activate on Chrome browser
Sleep 5 # Interval (in seconds) between switch 
$wshell.SendKeys("{F6}") # F6 Select Address Bar
Sleep 1 # Interval (in seconds) between switch 
$wshell.SendKeys("^c")
Sleep 1 # Interval (in seconds) between switch 
$MyURL = Get-Clipboard
"$MyURL"
$PSId = (Get-process -name "PowerShell*").ID
Show-Process -Process (Get-Process -Id $PSId) -Maximize
Remove-Variable wshell

Results:

https://stackoverflow.com/questions/67297306/powershell-get-url-of-browser-tab
PS> 

HTH

RetiredGeek
  • 2,980
  • 1
  • 7
  • 21
  • Hello RetiredGeek, thank you very much. I have noticed that if the Chrome browser is open on the second monitor and minimized, the script cannot maximize it. Do you have a solution for that as well maybe? – A. Ö. May 03 '21 at 06:57
0

Added to RetiredGeek's great answer.

Copy his code and add this to the end. Using sendkeys is not pretty. If you have multiple chrome windows open you'll need to alt tab yourself with code as is. Split your screen so you can see both PowerShell and chrome window. Let it run through chrome tabs a couple of times and just select -unique to remove dups. Don't do anything else while this runs as sendkeys will send those keys to whatever app is in focus that instant. Once all tabs show in PS console then switch to PS and press ctrl-c. $results will have the URLs from tabs.

Function LoopThruTabs {
Clear-Host
$results=@()
Set-Clipboard -Value $null
$wshell=New-Object -ComObject wscript.shell
$Null = $wshell.AppActivate('Chrome') # Activate on Chrome browser
Sleep 5 # Interval (in seconds) between switch 
While($true){
$wshell.SendKeys("^{TAB}") # TAB Switch chrome tab
Sleep 2 # Interval (in seconds) between switch 
$wshell.SendKeys("{F6}") # F6 Select Address Bar
Sleep 1 # Interval (in seconds) between switch 
$wshell.SendKeys("^c")
Sleep 1 # Interval (in seconds) between switch 
$MyURL = Get-Clipboard
"$MyURL" ; $Global:results +=$MyURL
}
}

LoopThruTabs 
$results | select -Unique