Getting the Final redirected URL from within PowerShell

依然范特西╮ 提交于 2021-02-05 10:41:05

问题


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.


回答1:


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;
        }
    }



来源:https://stackoverflow.com/questions/63042390/getting-the-final-redirected-url-from-within-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!