0

我正在尝试下面的代码来制作一个运行手册,它会删除所有早于几天的文件共享。我知道“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
4

1 回答 1

0

关于这个问题,您可以使用 PowerShell cmdletGet-AzureStorageFile返回文件,然后运行命令$file.FetchAttributes()来重新调整文件的属性。这些属性包含LastModified

例如

$share=""
$accountName=""
$key=""
$context =New-AzureStorageContext -StorageAccountName $accountName -StorageAccountKey $key

#define function to list files in child dir
function listFiles([string]$path){ 
   $file1s=$null
   $file1s=Get-AzureStorageFile -ShareName $share -Context $context -Path $path | Get-AzureStorageFile  
    foreach($file1 in $file1s){     
      if($file1.GetType().name -eq "CloudFile"){
            $file1.FetchAttributes()
            Write-Output ("Last Modified Date of the file $($file1.Name) is $($file1.Properties.LastModified)") 
       }else{
                   
             listFiles("$($file1.Parent.Name)/$($file1.Name)")
       }

  } 
 
}


#list files in root dir
$files =Get-AzureStorageFile -ShareName $share -Context $context 


foreach($file in $files){
 if($file.GetType().name -eq "CloudFile"){
     $file.FetchAttributes()
     Write-Output ("Last Modified Date of the file $($file.Name) is $($file.Properties.LastModified)")
 
 }else{
   
     listFiles($file.Name)
  }

}

在此处输入图像描述

于 2021-02-04T05:35:51.277 回答