我正在尝试下面的代码来制作一个运行手册,它会删除所有早于几天的文件共享。我知道“LastModified”返回“Null”,它的工作方式与 blob 不同。相反,需要使用“获取文件元数据”并从中获取“LastModified”。但我不知道如何从文件列表的结果中获取“获取文件元数据”。
## Declaring the variables
$number_of_days_threshold = 20
$current_date = get-date
$date_before_blobs_to_be_deleted = $current_date.AddDays(-$number_of_days_threshold)
# Number of blobs deleted
$file_count_deleted = 0
# Storage account details
$storage_account_name = "xxx"
$storage_account_key = "xxx"
$ShareName = "xxx"
## Creating Storage context for Source, destination and log storage accounts
$context = New-AzureStorageContext -StorageAccountName $storage_account_name -StorageAccountKey $storage_account_key
$file_list = Get-AzureStorageFile -Context $context -ShareName $ShareName
$file_list2 = Get-AzureStorageFileContent -Context $context -ShareName $ShareName
## Creating log file
$log_file = "log-"+(get-date).ToString().Replace('/','-').Replace(' ','-').Replace(':','-') + ".txt"
$local_log_file_path = $env:temp + "\" + "log-"+(get-date).ToString().Replace('/','-').Replace(' ','-').Replace(':','-') + ".txt"
write-host "Log file saved as: " $local_log_file_path -ForegroundColor Green
## Iterate through each blob
foreach($file_iterator in $file_list2){
$file_date = [datetime]$file_iterator.LastModified.UtcDateTime
#$file_date = [datetime]$file_iterator.getFileMetadata.LastModified.UtcDateTime
# Check if the blob's last modified date is less than the threshold date for deletion
if($file_date -le $date_before_files_to_be_deleted) {
Write-Output "-----------------------------------" | Out-File $local_log_file_path -Append
write-output "Purging file from Storage: " $file_iterator.name | Out-File $local_log_file_path -Append
write-output " " | Out-File $local_log_file_path -Append
write-output "Last Modified Date of the Blob: " $file_date | Out-File $local_log_file_path -Append
Write-Output "-----------------------------------" | Out-File $local_log_file_path -Append
# Cmdle to delete the blob
Remove-AzureStorageFile -ShareName $ShareName -path $file_iterator.Name -Context $context
$file_count_deleted += 1
}
}
write-output "Files deleted: " $file_count_deleted | Out-File $local_log_file_path -Append