且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从PowerShell中获取最终重定向的URL

更新时间:2023-11-26 16:36:58

用户个人资料中的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;
        }
    }