gpt4 book ai didi

powershell - 变量在不应该发生变化时发生变化

转载 作者:行者123 更新时间:2023-12-03 00:30:07 25 4
gpt4 key购买 nike

我有一个非常简单的 PS 脚本设置为采用 3 个参数(远程 HTTP 文件、本地复制路径、所需文件的过滤器列表),然后下载请求的文件。我正在使用 System.Net.WebClient和方法DownloadFile .我目前面临一个非常奇怪的错误:

Exception calling "DownloadFile" with "2" argument(s): "Could not find a part of the path 'C:\Users\lrichards\Desktop\US-LMI\PowerShell\BLS_Downloads\download.bls.gov\pub\time.series\la\la.area_type'." At C:\Users\lrichards\Desktop\US-LMI\PowerShell\BLS_Downloads\HTTP_based_downloader.ps1:45 char:13 + $wc.DownloadFile($sourceFile, $targetFile); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException



但是如果我 Write-Host在调用下载文件之前和之后,目标路径是有效的。当我调试该行时,它确实具有正确的路径。

正确的值应该是:
C:\Users\lrichards\Desktop\US-LMI\PowerShell\BLS_Downloads\la\la.area_type

但它正在某处更改为:
C:\Users\lrichards\Desktop\US-LMI\PowerShell\BLS_Downloads\download.bls.gov\pub\time.series\la\la.area_type
$sourceFile设定为:
download.bls.gov/pub/time.series/la/la.area_type

这是违规行:
$wc.DownloadFile($sourceFile, $targetFile);

完整代码:
param(
# Required. Download source location (website).
[Parameter(Mandatory=$True)]
[string] $URL,
# Required. Download target location (local drive).
[Parameter(Mandatory=$True)]
[string] $destinationFolder,
# Required. File containing list of files to download. Must exist in same location as this script.
[Parameter(Mandatory=$True)]
[string] $fileList
)

process
{
# Setup params
if (!($destinationFolder.EndsWith("\"))) {
$destinationFolder = $destinationFolder + "\";
}

if (!($URL.EndsWith("/"))) {
$URL = $URL + "/";
}

# Get root domain for use in determining $soureFile
[uri]$URL = $URL;
$baseURL = $URL.Authority;

# Get list of files requested from filter list file.
$filesRequested = gc $fileList;

# Set up the web client for downloading the files.
$wc = New-Object System.Net.WebClient;

# Get list of files available at $URL
$fileItems = Invoke-WebRequest -uri $URL | select -expand links | select -expand href;

# Loop through list of available files and find matches in filter list.
$fileItems | foreach {
if ($filesRequested.Contains((split-path $_ -Leaf))) {
$sourceFile = $baseURL + $_;
$targetFile = $destinationFolder + (split-path $_ -Leaf);
Write-Host $sourceFile;
Write-Host $targetFile;
#Write-Host "Getting: "$sourceFile "Destination: "$targetFile;
$wc.DownloadFile($sourceFile, $targetFile);
}
}
}

我是这样称呼它的:
.\HTTP_based_downloader.ps1 "http://download.bls.gov/pub/time.series/la/" "C:\Users\lrichards\Desktop\US-LMI\PowerShell\BLS_Downloads\la\" "la-file-list.txt"
la-file-list.txt的内容:

la.area la.area_type la.contacts

最佳答案

该错误是由无效的源路径引起的。$url.Authority value 不包含协议(protocol)名称,这使 value 成为字符串而不是 URI。

这使得 webclient 使用 DownloadFile(string,string)使用相对本地路径而不是 Downloadfile(uri,string) 作为源重载.上述脚本的解决方案类似于:

$baseurl = "$($URL.Scheme)://$($URL.Authority)"

关于powershell - 变量在不应该发生变化时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21030096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com