You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell提取.url快捷方式的URL并下载PDF文件

从.url快捷方式提取URL并下载PDF的解决方案

问题说明

需在源、目标服务器间复制PDF文件,但源服务器仅存有指向网页地址的FileName.pdf.url快捷方式,网络共享映射为D:和E:。执行Copy-Item -Path D:\FileName.pdf.url -Destination E:\FileName.pdf仅复制快捷方式,无法获取实际PDF文件。Windows资源管理器的「Web内容」标签可查看目标URL(示例:https://ServerName.int:11443/AFUWeb/RetrieveDocument.do?r=ieXZcv-OHZHB465.pdf),但使用Get-ChildItem、Get-ItemProperty无法提取该URL信息。环境为PowerShell 5.1、Windows Server 2012,已掌握PDF下载方法,需将提取到的URL作为变量传入DownloadFile方法。

提取.url文件中的URL

.url本质是INI格式的文本文件,内部包含[InternetShortcut]节和URL=目标地址的行,可通过读取文件内容并匹配正则表达式提取URL:

# 指定.url文件路径
$urlFilePath = "D:\FileName.pdf.url"

# 读取文件内容并提取URL
$urlContent = Get-Content -Path $urlFilePath -Raw
$urlMatch = [regex]::Match($urlContent, 'URL=(.*)')

if ($urlMatch.Success) {
    $downloadUrl = $urlMatch.Groups[1].Value
} else {
    Write-Error "无法从.url文件中提取目标URL"
    exit 1
}

下载PDF到目标共享盘

使用System.Net.WebClient将PDF下载到目标路径E:,直接传入提取到的$downloadUrl变量:

# 初始化WebClient对象
$webClient = New-Object System.Net.WebClient

try {
    # 执行下载
    $webClient.DownloadFile($downloadUrl, "E:\FileName.pdf")
    Write-Host "PDF文件已成功下载至E:\FileName.pdf"
} catch {
    Write-Error "下载失败: $_"
} finally {
    # 释放资源
    $webClient.Dispose()
}

完整脚本

将提取URL和下载步骤整合为完整脚本,方便直接执行:

# 配置路径参数
$urlFilePath = "D:\FileName.pdf.url"
$targetFilePath = "E:\FileName.pdf"

# 提取URL
$urlContent = Get-Content -Path $urlFilePath -Raw
$urlMatch = [regex]::Match($urlContent, 'URL=(.*)')

if (-not $urlMatch.Success) {
    Write-Error "提取URL失败,请检查.url文件是否有效"
    exit 1
}
$downloadUrl = $urlMatch.Groups[1].Value

# 下载PDF文件
$webClient = New-Object System.Net.WebClient
try {
    $webClient.DownloadFile($downloadUrl, $targetFilePath)
    Write-Host "下载完成,文件路径:$targetFilePath"
} catch {
    Write-Error "下载过程出错: $_"
} finally {
    $webClient.Dispose()
}

内容的提问来源于stack exchange,提问作者Edyward

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 04:55:22