繁体   English   中英

使用 Runbook 从 Azure 文件存储中删除超过 1 年的文件

[英]Delete files older than 1 year from Azure file storage with Runbook

我正在努力修改 Azure 中的 powershell Runbook 以删除超过 365 天的文件,我在下面有这个,但它只从顶级文件夹中删除文件,我还需要从子文件夹中删除。

感谢您的任何建议!

$DaysOld = 365
$connectionName = 'xxxxxxx'
$resourceGroupName = 'xxxxxx'
$subscriptionName = 'xxxxxxx' 
$storageAccountName = 'xxxxxxx'
$containerName = 'xxxxxxx'
Get-AzureRmSubscription -SubscriptionName $subscriptionName | Select-AzureRmSubscription
Set-AzureRmCurrentStorageAccount -ResourceGroupName $resourceGroupName -StorageAccountName 
$storageAccountName
$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName - 
StorageAccountName $storageAccountName).Key1
$storageAccountContext = New-AzureStorageContext -StorageAccountName $storageAccountName - 
StorageAccountKey $storageAccountKey

$storageShare = Get-AzureStorageShare -Name $containerName
$wsitedataDirectories = Get-AzureStorageFile -Share $storageShare | where-object 
{$_.GetType().Name -eq "CloudFileDirectory"}


$filesDeletedCount = 0
$filesDeletedSize = 0
$filesChecked = 0
foreach ($wsitedataDirectory in $wsitedataDirectories)
{
$wsitedataFiles = Get-AzureStorageFile -Directory $wsitedataDirectory
foreach ( $wsitedata in $wsitedataFiles)
{
    $filesChecked = $filesChecked + 1
    $wsitedata.FetchAttributes()
    if ($wsitedata.Properties.LastModified -le (Get-date).AddDays(-1*$DaysOld))
    {
         Write-Output ("File for deletion: " + $wsitedata.Name)
         $filesDeletedSize = $filesDeletedSize + $wsitedata.Properties.Length
         #Remove-AzureStorageFile -File $wsitedata -Confirm:$false
         $filesDeletedCount = $filesDeletedCount + 1
    }

}
}

要从子文件夹中删除文件,可以使用递归方法。
在执行删除之前,请查看Microsoft 文档以了解最佳做法

请遵循以下脚本:

$DaysOld = 365
$ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey 
$key
$shareName = <shareName>

# used for storing directories    
$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

$filesDeletedCount = 0
$filesDeletedSize = 0
$filesChecked = 0

# List files recursively and remove file older than 365 days 
While ($dirsToList.Count -gt $DirIndex)
{
 # accessing the directory stored in the list
 $dir = $dirsToList[$DirIndex]

 # increment the count of directories
 $DirIndex ++

 # list of files in the directory
 $fileListItems = $dir | Get-AzStorageFile

 # list the sub directories of this directory in a list
 $dirsListOut = $fileListItems | where {$_.GetType().Name -eq 
 "AzureStorageFileDirectory"}

 # add the sub directories to the list
 $dirsToList += $dirsListOut
 $files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}

 foreach($file in $files)
 {
$filesChecked = $filesChecked + 1
$file.FetchAttributes()
if ($file.Properties.LastModified -le (Get-date).AddDays(-1*$DaysOld))
    {
    Write-Output ("File for deletion: " + $file.Name)
        $filesDeletedSize = $filesDeletedSize + $file.Properties.Length
        #remove the file
        $filesDeletedCount = $filesDeletedCount + 1
     }
   }
}

资料来源: Deherman-MSFT回答的类似MS 问答

暂无
暂无

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

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