在Powershell中通过JSON进行迭代



我正在编写一个脚本,该脚本将列出网站上的文件,并使用JSON响应下载它们,然后删除它们(以下代码中未包含(。我粘贴在下面的代码允许我列出工作区中的文件,然后下载它们。我遇到的问题是它正在覆盖文件。

例如,我在下载的网站上有两个文件——File1.zip和File2.zip。File1包含一个显示HI的文本文件,File2包含一个表示BYE的文本文件。运行下面的代码后,会下载两个文件,分别命名为File1.zip和File2.zip,但它们都包含File2.zip的内容;C: \CRFDontDelete$id-$name";,然后下载4个文件,前两个包含File1.zip的内容,另外两个包含文件2.zip的内容。

不包括脚本的其他部分,我在其中初始化和设置了一些签名要求。这与此无关,因为该部分运行良好。关于我做错了什么和/或我可以做什么不同的事情以正确下载这两个文件,有什么想法吗?仅供参考,在我下载文件后,我需要删除它们,这样下次文件出现时,它们将有不同的ID和名称(这是我正在进行的自动化过程(。

#region Retrieve File List
$DateTime = (Get-Date).ToUniversalTime()
$fileUnixTimestamp = [System.Math]::Truncate((Get-Date -Date $DateTime -UFormat %s))
#$fileUnixTimestamp = [System.Math]::Truncate((Get-Date -Date((Get-Date).ToUniversalTime()) -UFormat %s))
$fileBaseURL = "https://upload.filegenius.com/api/file_list?api_key=" + $apiKey + "&path=" + $path + "&timestamp=" + $fileUnixTimestamp
$fileMessageSignature = [System.Bitconverter]::ToString($hmacsha.ComputeHash([Text.encoding]::UTF8.GetBytes($fileBaseURL.ToString()))).Replace("-", [System.String]::Empty).ToLower()
$fileSignedURL = $fileBaseURL + "&signature=" + $fileMessageSignature
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$fileGetResponse = Invoke-RestMethod -Uri $fileSignedURL -Method GET
#$fileInfo = (($fileGetResponse.fileList.id | ConvertTo-Json) | ConvertFrom-Json) -Depth 5
$fileInfo = $fileGetResponse.fileList.files.id | ConvertTo-Json -Depth 5
$fileNames = $fileGetResponse.fileList.files.name | ConvertTo-Json -Depth 5
#write-host "######################################################################################################################"
write-host $fileInfo
#endregion
# Now we've listed all of the files, we need to loop through and download them by ID
$fileIDs = $fileInfo | ConvertFrom-Json
write-host $fileIDs #Output shows: 35011794 35009511     
$fileName = $fileNames | ConvertFrom-Json
write-host $fileName #Output shows:  File1.zip File2.zip   
#region Download Files
#$fileidnum = $fileGetResponse.filesList.files.id
foreach($id in $fileIDs)
{
write-host $id
write-host "------------"
$testID = "35011794"
$downloadUnixTimestamp = [System.Math]::Truncate((Get-Date -Date ((Get-Date).ToUniversalTime()) -UFormat %s))
$downloadBaseURL = "https://upload.filegenius.com/api/file_download?api_key=" + $apiKey + "&file_id=" + $id + "&timestamp=" + $downloadUnixTimestamp
$downloadSignature = [System.BitConverter]::ToString($hmacsha.ComputeHash([Text.encoding]::UTF8.GetBytes($downloadBaseURL.ToString()))).Replace("-", [System.String]::Empty).ToLower()
$downloadSignedURL = $downloadBaseURL + "&signature=" + $downloadSignature


#write-host "######################################################################################################################"
#write-host $downloadSignedURL
#write-host "######################################################################################################################"
foreach($name in $fileName)
{
$outFilePath = "C:CRFDontDelete$name"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$fileDownload = Invoke-RestMethod -Uri $downloadSignedURL -Method 'Get' -Outfile $outFilePath
}
}

Nevermind,在对它进行修补后,我发现了它。我设置了一个名为$fileIDName的变量,并将其设置为$fileName[x],对于每个变量,我将x设置为0,然后在下载文件后,将1添加到x。现在,它正在正确地迭代并提取相关的文件ID和文件名,并下载它们。

您正在循环遍历每个输入id的两个文件名。看起来第二个输入id的结果覆盖了第一个。

这是您的代码摘要-

foreach($id in $fileIDs)
{
#create url and signature here
foreach($name in $fileName) #Download to both paths using the one URL created earlier
{
$outFilePath = "C:CRFDontDelete$name"
}
}

最新更新