Compartilhar via


Azure - PowerShell setting Blob Tiers

 function Set-AgedBlobsToTier  {
 PARAM (
       [Parameter(Mandatory = $true)] [int] $AgeInDays,
       [Parameter(Mandatory = $true)] [Microsoft.WindowsAzure.Storage.Blob.Standardblobtier] $StorageTier,
       [Parameter(Mandatory = $true)] [string]$ResourceGroupName,
       [Parameter(Mandatory = $true)] [string]$StorageAccountName
      )
     $StorageAccountName = $StorageAccountName.ToLower()

     if  (Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -EA SilentlyContinue)  {

       $StorageAccountKeys    = Get-AzureRmStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
       $StorageAccountKey     = $StorageAccountKeys[0].Value
       $StorageAccountContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
       $StorageContainers     = Get-AzureStorageContainer -Context $StorageAccountContext

       #retrieve all blobs from containers
       $Blobs = @()
       foreach($StorageContainer in $StorageContainers) {
         $Blobs += Get-AzureStorageBlob -Context $StorageAccountContext -Container $StorageContainer.Name
       }

       $RetentionDate = (Get-Date).ToUniversalTime().AddDays(-$AgeInDays)
       $ifNotModifiedSinceTime=[Microsoft.WindowsAzure.Storage.AccessCondition]::GenerateIfNotModifiedSinceCondition( $RetentionDate )

       # now apply the tier
       Foreach($Blob in $Blobs) {
         Write-Verbose -Message ('checking {0}' -f  $Blob.ICloudBlob.Uri.AbsoluteUri)
         $Blob.ICloudBlob.SetStandardBlobTier($StorageTier,$ifNotModifiedSinceTime)
       }
    }
    else {
      Write-Error -Message ('{0} was not found' -f $StorageAccountName)
    }

}

 Set-AgedBlobsToTier -AgeInDays 30 -StorageTier Cool -ResourceGroupName 'testing' -StorageAccountName 'somenamehere'  -Verbose
 

Comments