批处理之家's Archiver

5i365 发表于 2022-5-8 08:18

如何用Powershell获取url跳转后的实际链接

[i=s] 本帖最后由 5i365 于 2022-5-8 08:33 编辑 [/i]

下面这两个链接, 在浏览器打开后, 地址栏上的链接会变成实际的链接, 如何直接取到这个实际链接的值?
百度没有搜索到相关的资料, 求路过大侠指引, 提前感谢

[url=http://go.microsoft.com/fwlink/?LinkID=113392]http://go.microsoft.com/fwlink/?LinkID=113392[/url]
[url=https://www.kugou.com/mixsong/29008597.html]https://www.kugou.com/mixsong/29008597.html[/url]

找了一段代码, 也没有结果,打不开IE会打开默认的
$IE = new-object -com internetexplorer.application
$IE.visible = $true
$Document = $IE.navigate2("http://go.microsoft.com/fwlink/?LinkID=113392")
# Do stuff
$OldUrl = $IE.Document.url
$Document = $IE.navigate2($LinkIFound)
sleep -seconds 3
$NewUrl = $IE.Document.url
$NewUrl
看到有文章用三行python代码搞定, ps有类似的函数吗?
[url=https://blog.csdn.net/m0_46521785/article/details/109684811?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-109684811-blog-46471409.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-109684811-blog-46471409.pc_relevant_default&utm_relevant_index=2]https://blog.csdn.net/m0_46521785/article/details/109684811?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-109684811-blog-46471409.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-109684811-blog-46471409.pc_relevant_default&utm_relevant_index=2[/url]

5i365 发表于 2022-5-8 09:05

国外找到一个函数, 但是只有一个链接有结果, 而且结果也不对[code]
Function Get-RedirectedUrl
{
        Param (
                [Parameter(Mandatory = $true)]
                [String]$URL
        )
        
        $request = [System.Net.WebRequest]::Create($url)
        $request.AllowAutoRedirect = $false
        $response = $request.GetResponse()
        
        If ($response.StatusCode -eq "Found")
        {
                $response.GetResponseHeader("Location")
        }
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Get-RedirectedUrl "https://www.kugou.com/mixsong/29008597.html"
Get-RedirectedUrl "http://go.microsoft.com/fwlink/?LinkID=113392"
[/code]

went 发表于 2022-5-8 15:36

[code]cls
$url = 'http://go.microsoft.com/fwlink/?LinkID=113392'
$resp = Invoke-WebRequest -UseBasicParsing -Uri $url
$resp.BaseResponse.ResponseUri.AbsoluteUri[/code]

5i365 发表于 2022-5-8 17:22

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=255451&ptid=62657]3#[/url] [i]went[/i] [/b]


   感谢大侠帮忙, 另一个链接没有找到实际链接
$url = "https://www.kugou.com/mixsong/29008597.html"
$resp = Invoke-WebRequest -UseBasicParsing -Uri $url
$resp.BaseResponse.ResponseUri.AbsoluteUri

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.