【问题标题】:Azure Runbook not deleting Files in FileshareAzure Runbook 不删除文件共享中的文件
【发布时间】:2021-10-20 17:17:34
【问题描述】:

我正在尝试使用 Azure Powershell Runbooks 从 Azure 文件共享中删除文件。没有返回错误,但文件没有被删除。自动化帐户具有未过期的运行方式帐户设置或任何内容,如果我从本地计算机运行该脚本,则该脚本可以工作。在这方面寻求一些建议。

$ctx = New-AzureStorageContext -StorageAccountName "" -StorageAccountKey "" 
$shareName = ""
$directoryPath = ".cloudconsole"
$DirIndex = 0
$day = 1
$startdate = (Get-Date).AddDays(-180)
$endDate = (Get-date).AddDays(-32)

$dirsToList = New-Object System.Collections.Generic.List[System.Object]


$shareroot = Get-AzureStorageFile -ShareName $shareName -Path $directoryPath -context $ctx 
$dirsToList += $shareroot 
While ($dirsToList.Count -gt $DirIndex)
{
 $dir = $dirsToList[$DirIndex]
 $DirIndex ++
 $fileListItems = $dir | Get-AzureStorageFile
 $dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}
 $dirsToList += $dirsListOut
 $files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}

 foreach($file in $files)
 {
   
     $task = $file.CloudFile.FetchAttributesAsync()
     $task.Wait()

   
        if ($file.CloudFile.Properties.LastModified -ge $startdate -and $file.CloudFile.Properties.LastModified -ge $endDate  )

     {
     if ($file.CloudFile.Properties.LastModified.day -ne '01'  )
     
        {
         $file | Remove-AzureStorageFile
         }
     }
        if ($file.CloudFile.Properties.LastModified -lt $startdate)
        
     {
     
        
         $file | Remove-AzureStorageFile 
     }
   
 }


 }

【问题讨论】:

  • 你能说一下为什么在 if 循环中,“ge”(大于或等于)在开始日期和结束日期都提到了这样的 >> if ($file.CloudFile.Properties.LastModified - ge $startdate -and $file.CloudFile.Properties.LastModified -ge $endDate) ?如果我没有错,那不应该是'-le'和'ge'吗?如果您希望删除超过 32 天的文件共享,请参考this
  • 问题解决了吗?

标签: azure-storage azure-powershell azure-automation azure-files azure-runbook


【解决方案1】:

BoxJumper:正如kavyasaraboju-MT 所指,下面的脚本应该对您有所帮助。

$ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $key
$shareName = <shareName>
$startdate = (Get-Date).AddDays(-180)
$endDate = (Get-date).AddDays(-32)
    
 $DirIndex = 0
 $dirsToList = New-Object System.Collections.Generic.List[System.Object]
    
 # Get share root Dir
 $shareroot = Get-AzStorageFile -ShareName $shareName -Path . -context $ctx 
 $dirsToList += $shareroot 
    
 # List files recursively and remove file older than 14 days 
 While ($dirsToList.Count -gt $DirIndex)
 {
     $dir = $dirsToList[$DirIndex]
     $DirIndex ++
     $fileListItems = $dir | Get-AzStorageFile
     $dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}
     $dirsToList += $dirsListOut
     $files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}
    
     foreach($file in $files)
     {
         # Fetch Attributes of each file and output
         $task = $file.CloudFile.FetchAttributesAsync()
         $task.Wait()
    
         # remove file if it's modified between after last 180 days and before last 30 Days
         if ($file.CloudFile.Properties.LastModified -lt (Get-Date).AddDays(-32) -and $file.CloudFile.Properties.LastModified -ge (Get-Date).AddDays(-180))
         {
             ## print the file LMT
             # $file | Select @{ Name = "Uri"; Expression = { $_.CloudFile.SnapshotQualifiedUri} }, @{ Name = "LastModified"; Expression = { $_.CloudFile.Properties.LastModified } } 
    
             # remove file
             $file | Remove-AzStorageFile
         }
     }
     #Debug log
     # Write-Host  $DirIndex $dirsToList.Length  $dir.CloudFileDirectory.SnapshotQualifiedUri.ToString() 
 } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-19
    • 2021-05-08
    • 2019-05-18
    • 1970-01-01
    • 2020-09-30
    • 2021-12-06
    • 2020-10-07
    • 2021-12-01
    相关资源
    最近更新 更多