从PowerShell中获取最终重定向的URL [英] Getting the Final redirected URL from within PowerShell

查看:135
本文介绍了从PowerShell中获取最终重定向的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在找到类似的答案,但并不是我所需要的.

背景故事:我正在尝试编写一个Powershell脚本,该脚本将抓取Chrome和Microsoft Edge(基于铬)的扩展目录,并将名称,ID和版本输出到自定义WMI类中,以便我的端点管理代理可以在库存记录中准确反映用户安装的扩展程序.

在Chrome中,即使manifest.json不包含人类可读的名称",这也相当容易.属性,因为您只需获取ID(该扩展名的顶级文件夹)并运行以下命令即可:

<#
# Chrome URL's are "https://chrome.google.com/webstore/detail/" + obj.ID
# Edge URL's are "https://microsoftedge.microsoft.com/addons/detail/" + obj.ID
#>

$url = "https://chrome.google.com/webstore/detail/" + obj.ID
$webclient = New-Object System.Net.WebClient
try
{
   $data = $wc.downloadstring($url)    
   $titletag = [regex] '(?<=<title>)([\S\s]*?)(?=</title>)' 
   $title = $titletag.Match($data).value.trim()    
   $obj.Name = $title
}
catch{
   $obj.Name = "Unknown - Please search manually"
}

当您将以上链接放在浏览器地址栏中时,Edge和Chrome网络存储都将重定向(例如,

但是

Edge不会,并且它作为标题标签返回的唯一内容是"Microsoft Edge插件"

我试图尝试使用以下代码找到重定向的URL:

Function Get-RedirectedUrl {
    Param (
        [Parameter(Mandatory=$true)]
        [String]$url
    )

    $request = [System.Net.WebRequest]::Create($url)
    $request.AllowAutoRedirect=$true
    $request.UserAgent = 'Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0 Safari/534.6'
    
    try
    {
        $response = $request.GetResponse()
        $response.ResponseUri.AbsoluteUri
        $response.Close()
    }
    catch
    {
        "Error: $_"
    }
}

$url = 'https://microsoftedge.microsoft.com/addons/detail/gmgoamodcdcjnbaobigkjelfplakmdhh'

Get-RedirectedUrl -URL $url

但是唯一要做的是吐出与我提供的URL相同的URL,所以我猜测重定向是在JavaScript内发生的,但我不知道它是如何做到的.

如果有更好的方法来尝试扩展的名称(不依赖manifest.json,因为其中一些是这样的

名称":" MSG_name_releasebuild ",

如果您有任何建议,我绝对愿意接受.否则,我立即知道的唯一方法就是尝试从可从Web客户端下载的实际HTML中获取标题.

我唯一真正的警告是,要完成我所需要的工作,我将尽可能选择保留在PowerShell领域之内.

解决方案

用户个人资料中的Extensions/目录不包含解包的开发人员模式扩展,并且您发现提取名称存在问题.

解决方案:

  1. 读取并解析Secure PreferencesPreferences文件
  2. 普通扩展名在manifest属性内具有易于理解的nameversion
  3. 解压缩的扩展程序需要直接读取其manifest.json,并可以选择从扩展程序的_locales目录中扩展消息ID.

 'Secure Preferences', 'Preferences' |
    %{ gc ($env:LOCALAPPDATA + '/Google/Chrome/User Data/Default/' + $_) -raw } |
    ConvertFrom-Json |
    ?{ $_.extensions.settings } |
    %{ $_.extensions.settings.PSObject.Properties } |
    %{
        $info = $_.Value
        $name = $info.manifest.name
        $version = $info.manifest.version
        if (!$name -or !$version) {
            Push-Location $info.path
            $manifest = gc 'manifest.json' -raw | ConvertFrom-Json
            $name = $manifest.name
            $version = $manifest.version
            if ($name -cmatch '^__MSG_(\w+?)__$') {
                $msgId = $Matches[1]
                $msgFile = '_locales/' + $manifest.default_locale + '/messages.json'
                $name = (gc -raw $msgFile | ConvertFrom-Json).$msgId.message
            }
            Pop-Location
        }
        [PSCustomObject]@{
            id=$_.Name;
            name=$name;
            version=$version;
        }
    }

 

I'm finding similar answers, but not exactly what I'm needing.

The backstory: I'm trying to write a powershell script that will scrape the Chrome and Microsoft Edge (chromium-based) extensions directory and output the name, ID, and version into a custom WMI class so that my endpoint management agent can accurately reflect user-installed extensions within the inventory record.

In Chrome, this is reasonably easy even if the manifest.json doesn't include a human-readible "name" property, because you can simply take the ID (which is the top level folder of that extension) and run the following:

<#
# Chrome URL's are "https://chrome.google.com/webstore/detail/" + obj.ID
# Edge URL's are "https://microsoftedge.microsoft.com/addons/detail/" + obj.ID
#>

$url = "https://chrome.google.com/webstore/detail/" + obj.ID
$webclient = New-Object System.Net.WebClient
try
{
   $data = $wc.downloadstring($url)    
   $titletag = [regex] '(?<=<title>)([\S\s]*?)(?=</title>)' 
   $title = $titletag.Match($data).value.trim()    
   $obj.Name = $title
}
catch{
   $obj.Name = "Unknown - Please search manually"
}

While both Edge and Chrome webstores will redirect when you put the above links in a browser address bar (example, https://microsoftedge.microsoft.com/addons/detail/ipnhpapgpnoadggfoepbedokgppcodkl redirects to https://microsoftedge.microsoft.com/addons/detail/notifier-for-github/ipnhpapgpnoadggfoepbedokgppcodkl), Google actually makes this easier as they include the extension's title in the tag on the HTML without the redirect necessary.

So in the above example, running that code against Google's webstore would result in "Notifier for GitHub - Chrome Web Store"

Edge, however, does not, and the only thing it returns as the title tag is "Microsoft Edge Addons"

I have attempted to try to find the redirected URL with this code:

Function Get-RedirectedUrl {
    Param (
        [Parameter(Mandatory=$true)]
        [String]$url
    )

    $request = [System.Net.WebRequest]::Create($url)
    $request.AllowAutoRedirect=$true
    $request.UserAgent = 'Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0 Safari/534.6'
    
    try
    {
        $response = $request.GetResponse()
        $response.ResponseUri.AbsoluteUri
        $response.Close()
    }
    catch
    {
        "Error: $_"
    }
}

$url = 'https://microsoftedge.microsoft.com/addons/detail/gmgoamodcdcjnbaobigkjelfplakmdhh'

Get-RedirectedUrl -URL $url

But the only thing that does is spit out the same URL I supplied it, so I'm guessing that the redirection happens within JavaScript somehow, but I have no idea how it's doing that.

If there are better ways to try to scrape the extensions' names (that doesn't rely on the manifest.json, because some of them are like this

"name": "MSG_name_releasebuild",

I'm definitely open to that if you have suggestions. Otherwise, the only way I know right off hand is to try to grab the title from the actual HTML that can be downloaded from the webclient.

My only real caveat is that to accomplish what I am needing, I would greatly prefer to stay within the realms of PowerShell if at all possible.

解决方案

The Extensions/ directory in the user profile doesn't include the unpacked developer-mode extensions and as you discovered there's a problem of extracting the name.

Solution:

  1. read and parse the Secure Preferences or Preferences file
  2. normal extensions have a human-readable name and version inside manifest property
  3. unpacked extensions need reading their manifest.json directly and optionally expanding the message id from extension's _locales directory.

'Secure Preferences', 'Preferences' |
    %{ gc ($env:LOCALAPPDATA + '/Google/Chrome/User Data/Default/' + $_) -raw } |
    ConvertFrom-Json |
    ?{ $_.extensions.settings } |
    %{ $_.extensions.settings.PSObject.Properties } |
    %{
        $info = $_.Value
        $name = $info.manifest.name
        $version = $info.manifest.version
        if (!$name -or !$version) {
            Push-Location $info.path
            $manifest = gc 'manifest.json' -raw | ConvertFrom-Json
            $name = $manifest.name
            $version = $manifest.version
            if ($name -cmatch '^__MSG_(\w+?)__$') {
                $msgId = $Matches[1]
                $msgFile = '_locales/' + $manifest.default_locale + '/messages.json'
                $name = (gc -raw $msgFile | ConvertFrom-Json).$msgId.message
            }
            Pop-Location
        }
        [PSCustomObject]@{
            id=$_.Name;
            name=$name;
            version=$version;
        }
    }

这篇关于从PowerShell中获取最终重定向的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆