从使用Runbook的Azure文件存储中删除1岁以上的文件

发布于 2025-02-03 04:52:17 字数 1610 浏览 4 评论 0原文

我正在努力在Azure中修改PowerShell Runbook以删除超过365天的文件,我有以下内容,但它仅从Top文件夹中删除文件,我还需要从子文件夹中删除。

感谢您的建议!

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

}
}

I am struggling to modify powershell runbook in Azure to delete files older than 365 days, I have this below but it only delete files from top folder and I need to delete also from subfolders.

Thanks for any advice!

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

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生一个溪 2025-02-10 04:52:17

要从子文件夹中删除文件,您可以使用递归方法。
请检查 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
     }
   }
}

来源:相似 ms q&amp; a deherman-msft 回答。

To delete the files from the subfolders, you can use recursive approach.
Please check the Microsoft Documentation for the best practices before doing the delete.

Please follow the below script:

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

Source: Similar MS Q&A answered by Deherman-MSFT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文