繁体   English   中英

使用 powershell 脚本删除 azure 文件共享存储中的空文件夹

[英]Delete empty folders in azure file share storage using powershell script

我正在使用下面的脚本来删除 Azure 文件共享存储中超过 30 天的所有文件。 但是现在这个文件共享存储中有几个空文件夹和子文件夹,有没有办法删除我可以添加到脚本中的这些空文件夹/子文件夹?

$ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $key
 $shareName = <shareName>
    
 $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 30 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 older than 30 days.
         if ($file.CloudFile.Properties.LastModified -lt (Get-Date).AddDays(-30))
         {
             ## 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() 
 }

代码学分: https://learn.microsoft.com/en-us/answers/questions/482814/deleting-files-in-azure-file-share-older-than-x-da.html

您已经在$dirsListOut中获得了所有目录,只需创建另一个foreach循环并使用Remove-AzureStorageDirectory删除它们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM