[英]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.