[英]Iterating through JSON in Powershell
我正在编写一个脚本,该脚本将列出站点上的文件,并使用 JSON 响应下载它们然后删除它们(不包含在下面的代码中)。 我在下面粘贴的代码允许我列出工作区中的文件,然后下载它们。 我遇到的问题是它正在覆盖文件。
例如,我在下载的站点上有两个文件 - File1.zip 和 File2.zip。 File1 包含一个表示 HI 的文本文件,File2 包含一个表示 BYE 的文本文件。 运行以下代码后,下载了两个文件,分别命名为 File1.zip 和 File2.zip,但它们都包含 File2.zip 的内容。 如果我更改以下代码,使 $outFilePath 为“C:\\CRFDontDelete$id - $name”,则下载 4 个文件,第一个两个包含 File1.zip 的内容,另外两个包含 File2.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 + "×tamp=" + $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 + "×tamp=" + $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
}
}
没关系,经过修修补补,我想通了。 我设置了一个名为 $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"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.