简体   繁体   English

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

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

I'm using the script below in order to delete all the files in an Azure File Share Storage that are older than 30 days.我正在使用下面的脚本来删除 Azure 文件共享存储中超过 30 天的所有文件。 But now there are several empty folders and subfolders in this File Share Storage, is there a way to delete these empty folders/subfolders that I can add to the script?但是现在这个文件共享存储中有几个空文件夹和子文件夹,有没有办法删除我可以添加到脚本中的这些空文件夹/子文件夹?

$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() 
 }

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

you already got all directories in $dirsListOut , just create another foreach loop and delete them with Remove-AzureStorageDirectory您已经在$dirsListOut中获得了所有目录,只需创建另一个foreach循环并使用Remove-AzureStorageDirectory删除它们

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

相关问题 Azure 文件共享无法使用 powershell 脚本连接? - Azure file share can not connect using powershell script? 将文件上传到Azure存储文件共享的脚本 - Script to upload files to Azure Storage File Share 将文件上传到Azure文件存储的Powershell脚本 - Powershell Script That uploads files to Azure File Storage Powershell:将文件从 Azure 文件共享复制到 Blob 存储帐户 - Powershell: Copy file from Azure File Share to Blob Storage account 如何使用powershell ARM查找azure Storage帐户中的文件共享数量 - How to find how many file share in azure Storage account using powershell ARM 无法使用PowerShell在VM上映射Azure存储帐户文件共享 - Cannot get Azure Storage Account File Share mapped on VM using PowerShell 使用 PowerShell 获取 Azure 文件共享大小 - Get Azure File share size using PowerShell 用于从Azure文件共享还原Azure SQL数据库的PowerShell脚本 - PowerShell script for restoring the Azure SQL Database from Azure File share 带有Powershell脚本的Azure WebJob可将文件上传到Azure Blob存储 - Azure WebJob with Powershell Script to upload file to Azure Blob storage 删除 Azure 存储帐户中的 blob,使用 powershell 的最新版本除外 - Delete blobs in Azure storage account except the lastest version using powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM