从使用Runbook的Azure文件存储中删除1岁以上的文件
我正在努力在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要从子文件夹中删除文件,您可以使用递归方法。
请检查 Microsoft文档最佳实践进行删除之前。
请按照以下脚本:
来源:相似 ms q& 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:
Source: Similar MS Q&A answered by Deherman-MSFT.