Board logo

标题: [问题求助] 如何用Powershell获取url跳转后的实际链接 [打印本页]

作者: 5i365    时间: 2022-5-8 08:18     标题: 如何用Powershell获取url跳转后的实际链接

本帖最后由 5i365 于 2022-5-8 08:33 编辑

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

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

找了一段代码, 也没有结果,打不开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有类似的函数吗?
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
作者: 5i365    时间: 2022-5-8 09:05

国外找到一个函数, 但是只有一个链接有结果, 而且结果也不对
  1. Function Get-RedirectedUrl
  2. {
  3.         Param (
  4.                 [Parameter(Mandatory = $true)]
  5.                 [String]$URL
  6.         )
  7.         
  8.         $request = [System.Net.WebRequest]::Create($url)
  9.         $request.AllowAutoRedirect = $false
  10.         $response = $request.GetResponse()
  11.         
  12.         If ($response.StatusCode -eq "Found")
  13.         {
  14.                 $response.GetResponseHeader("Location")
  15.         }
  16. }
  17. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  18. Get-RedirectedUrl "https://www.kugou.com/mixsong/29008597.html"
  19. Get-RedirectedUrl "http://go.microsoft.com/fwlink/?LinkID=113392"
复制代码

作者: went    时间: 2022-5-8 15:36

  1. cls
  2. $url = 'http://go.microsoft.com/fwlink/?LinkID=113392'
  3. $resp = Invoke-WebRequest -UseBasicParsing -Uri $url
  4. $resp.BaseResponse.ResponseUri.AbsoluteUri
复制代码

作者: 5i365    时间: 2022-5-8 17:22

回复 3# went


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




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2