【发布时间】:2021-10-18 19:06:29
【问题描述】:
我正在尝试从 Azure 文件存储中删除当月第一天或更早的 30 + 1 的文件。 我有基本的列表并删除有效的脚本。我的主要问题是如何计算 if 早于语句?
$resourceGroupName=""
$storageAccName=""
$fileShareName=""
$directoryPath=""
## Function to Lists directories and files
Function GetFiles
{
Write-Host -ForegroundColor Green "Lists directories and files.."
## Get the storage account context
$ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context
## List directories
$directories=Get-AZStorageFile -Context $ctx -ShareName $fileShareName
## Loop through directories
foreach($directory in $directories)
{
write-host -ForegroundColor Magenta " Directory Name: " $directory.Name
$files=Get-AZStorageFile -Context $ctx -ShareName $fileShareName -Path $directory.Name | Get-AZStorageFile
## Loop through all files and display
foreach ($file in $files)
{
write-host -ForegroundColor Yellow $file.Name
}
}
}
GetFiles
$context = ""
Remove-AzStorageFile -ShareName "name" -Path "path" -Context $context
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
【问题讨论】:
-
您始终可以通过
[datetime]::Today.AddDays(([datetime]::Today.Day - 1)*-1)获得当月的第一天。我不熟悉“30 + 1”这个词。您只是在寻找超过一个月的文件吗?也许$file.properties.changetime -lt [datetime]::Today.AddDays(([datetime]::Today.Day - 1)*-1).addmonths(-1)或类似的东西?
标签: azure powershell azure-storage azure-powershell azure-files