######RemoveStorageBlob*DaysOld-ARM#####
<#
.SYNOPSIS
   Remove all blob contents from one storage account that are * days old.
.DESCRIPTION 
   This script will run through a single Azure storage account and delete all blob contents in 
   all containers, which are * days old.

   
#>

workflow delblob-arm-new
{
       param(
                #设置Org ID
                [parameter(Mandatory=$true)]
                [String]$AzureOrgId="***",
          
                #设置Org ID的密码
                [Parameter(Mandatory = $true)] 
                [String]$Password="***",
                
                #设置订阅名称
                [Parameter(Mandatory = $true)] 
                [String]$AzureSubscriptionName="***",

                #设置存储账号所在的资源组
                [Parameter(Mandatory = $true)]
                [String]$ResourceGroupName="***",
                
                #设置存储账号
                [Parameter(Mandatory = $true)]
                [String]$StorageAccountName="***",
                
                #设置Container Name
                [Parameter(Mandatory = $true)]
                [String]$ContainerName="***",
            
                #设置过期时间
                [Parameter(Mandatory = $true)] 
                [Int32]$DaysOld=30
    )

    $ChinaTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneByID("China Standard Time")
    $Start = [System.TimeZoneInfo]::ConvertTimefromUTC((get-date).ToUniversalTime(),$ChinaTimeZone)
    "Starting: " + $Start.ToString("HH:mm:ss.ffffzzz")


    $AzurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
    $AzureOrgIdCredential = New-Object System.Management.Automation.PSCredential($AzureOrgId,$AzurePassword)

    Add-AzureRmAccount -Credential $AzureOrgIdCredential -environmentname "AzureChinaCloud" | Write-Verbose
    
    # Set-AzureSubscription -SubscriptionName $AzureSubscriptionName -CurrentStorageAccountName $StorageAccountName
    
    Select-AzureRmSubscription -SubscriptionName $AzureSubscriptionName
    Set-AzureRmCurrentStorageAccount -ResourceGroupName $ResourceGroupName -StorageAccountName $StorageAccountName
    

    
    # loop through each container and get list of blobs for each container and delete
    $blobsremoved = 0
    $containers = Get-AzureStorageContainer -Name $ContainerName -ErrorAction SilentlyContinue

    #$storage = Get-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
    #write-output $storage
    #$context = [Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext]$storage.Context;
    #Write-Output($storage.Context.gettype());
    #$containers = Get-AzureStorageContainer -Name $ContainerName -Context $context -ErrorAction SilentlyContinue
    #$containers = $storage | Get-AzureStorageContainer -Name $ContainerName


    foreach($container in $containers) 
    { 
        $blobsremovedincontainer = 0       
        Write-Output ("Searching Container: {0}" -f $container.Name)   
        #$blobs = Get-AzureStorageBlob -Container $ContainerName -Context $context 
        #$blobs = $storage | Get-AzureStorageBlob -Container $ContainerName
        $blobs = Get-AzureStorageBlob -Container $ContainerName


        if ($blobs -ne $null)
        {    
            foreach ($blob in $blobs)
            {
               $lastModified = $blob.LastModified
               if ($lastModified -ne $null)
               {
                   $blobDays = ([DateTime]::Now - [DateTime]$lastModified)
                   Write-Output ("Blob {0} in storage for {1} days" -f $blob.Name, $blobDays) 
               
                   if ($blobDays.Days -ge $DaysOld)
                   {
                        Write-Output ("Removing Blob: {0}" -f $blob.Name)
                        Remove-AzureStorageBlob -Blob $blob.Name -Container $container.Name 
                        $blobsremoved += 1
                        $blobsremovedincontainer += 1
                   }
                }
            }
        }
        
        Write-Output ("{0} blobs removed from container {1}." -f $blobsremovedincontainer, $container.Name)       
    }
    
    $ChinaTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneByID("China Standard Time")
    $Finish = [System.TimeZoneInfo]::ConvertTimefromUTC((get-date).ToUniversalTime(),$ChinaTimeZone)
     
    $TotalUsed = $Finish.Subtract($Start).TotalSeconds
   
    Write-Output ("Removed {0} blobs in {1} containers in storage account {2} of subscription {3} in {4} seconds." -f $blobsRemoved, $containersremoved, $StorageAccountName, $AzureConnectionName, $TotalUsed)
    "Finished " + $Finish.ToString("HH:mm:ss.ffffzzz")
}

 

posted on 2018-07-12 16:29  铭的随记  阅读(240)  评论(0编辑  收藏  举报