微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PowerShell 获取浏览器选项卡的 URL

如何解决PowerShell 获取浏览器选项卡的 URL

我正在编写一个脚本,该脚本可用作使用 PowerShell 的支持票证创建者。当在任何用户的上下文中的任何随机时间激活时,该脚本将在 msedge、firefox 和 IE 中查找打开的浏览器选项卡,并在打开时获取焦点窗口的 URL。

我浏览了这个论坛和许多其他论坛,发现了许多负面回应,还有一个 link 正在谈论解决方案,但脚本的下载链接不再有效。

这就是为什么我想问:有什么解决办法吗?我的脚本已经获取了窗口标题属性

$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

非常感谢 奥

解决方法

此代码将返回 Chrome 中活动标签的网址。请注意,时机至关重要,需要在使用您的系统时进行试验。

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

结果:

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

HTH

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。