【发布时间】:2021-10-20 17:17:34
【问题描述】:
我正在尝试使用 Azure Powershell Runbooks 从 Azure 文件共享中删除文件。没有返回错误,但文件没有被删除。自动化帐户具有未过期的运行方式帐户设置或任何内容,如果我从本地计算机运行该脚本,则该脚本可以工作。在这方面寻求一些建议。
$ctx = New-AzureStorageContext -StorageAccountName "" -StorageAccountKey ""
$shareName = ""
$directoryPath = ".cloudconsole"
$DirIndex = 0
$day = 1
$startdate = (Get-Date).AddDays(-180)
$endDate = (Get-date).AddDays(-32)
$dirsToList = New-Object System.Collections.Generic.List[System.Object]
$shareroot = Get-AzureStorageFile -ShareName $shareName -Path $directoryPath -context $ctx
$dirsToList += $shareroot
While ($dirsToList.Count -gt $DirIndex)
{
$dir = $dirsToList[$DirIndex]
$DirIndex ++
$fileListItems = $dir | Get-AzureStorageFile
$dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}
$dirsToList += $dirsListOut
$files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}
foreach($file in $files)
{
$task = $file.CloudFile.FetchAttributesAsync()
$task.Wait()
if ($file.CloudFile.Properties.LastModified -ge $startdate -and $file.CloudFile.Properties.LastModified -ge $endDate )
{
if ($file.CloudFile.Properties.LastModified.day -ne '01' )
{
$file | Remove-AzureStorageFile
}
}
if ($file.CloudFile.Properties.LastModified -lt $startdate)
{
$file | Remove-AzureStorageFile
}
}
}
【问题讨论】:
-
你能说一下为什么在 if 循环中,“ge”(大于或等于)在开始日期和结束日期都提到了这样的 >> if ($file.CloudFile.Properties.LastModified - ge $startdate -and $file.CloudFile.Properties.LastModified -ge $endDate) ?如果我没有错,那不应该是'-le'和'ge'吗?如果您希望删除超过 32 天的文件共享,请参考this
-
问题解决了吗?
标签: azure-storage azure-powershell azure-automation azure-files azure-runbook