Skip to content

Instantly share code, notes, and snippets.

@marckean
Last active September 4, 2018 22:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marckean/cc7be4fb01a022caefb02dda43ec71e4 to your computer and use it in GitHub Desktop.
Save marckean/cc7be4fb01a022caefb02dda43ec71e4 to your computer and use it in GitHub Desktop.
#region Install Modules
# Run as Administrator
#Find-Module AzureServicePrincipalAccount | Install-Module
#endregion
# Retrieve Azure Module properties
""
"Validating installed PowerShell Version and Azure PowerShell Module version..."
$ReqVersions = Get-Module Azure -list | Select-Object Version, PowerShellVersion
# Current PowerShell version must be higher then the one required by the Azure Module
if($PSVersionTable.PSVersion.Major -lt $ReqVersions.PowerShellVersion.Major)
{
$PSVerReq = $ReqVersions.PowerShellVersion
$PSVerInst = $PSVersionTable.PSVersion
"Validation failed..."
"Installed PowerShell version: $PSVerInst"
"Powershell version $PSVerReq required. Please update the version of Powershell on this system"
"Exiting Script"
Break
}
# Current script was tested with Azure module 5.0.1
if($ReqVersions.Version.Major -lt 5)
{
$AZModuleInst = $ReqVersions.Version
"Validation failed..."
"Installed Azure PS Module: $AZModuleInst. This script was tested with version 5.0.1"
"Please download and install/update the Azure Powershell module using the Microsoft Web Platform Installer..."
"Download link: https://github.com/Azure/azure-powershell/releases/tag/v5.0.1-November2017"
"Exiting Script"
Break
}
##########################################################################################
################################# Logon to Azure ##################################
##########################################################################################
Login-AzureRmAccount
#region Logon to Azure & choose Azure subscription
$Subscription = (Get-AzureRmSubscription | Out-GridView -Title "Choose a Source & Target Subscription ..." -PassThru)
Select-AzureRmSubscription -Subscription $Subscription
$SubscriptionId = $Subscription.Id
#endregion
#### Question time ####
# Question 1 - Select Desired Storage Tier
$StorageTiers = @('Hot', 'Cool', 'Archive')
$StorageTier = ($StorageTiers | Out-GridView -Title "Select a storage tier ..." -PassThru)
# Question 2 - Storage Account
$StorageAccounts = Get-AzureRmStorageAccount |
Where-Object {($_.sku.tier -eq 'Standard') -and ($_.Kind -eq 'StorageV2' -or $_.Kind -eq 'BlobStorage')}
$StorageAccount = ($StorageAccounts | select StorageAccountName, PrimaryLocation, ResourceGroupName |
Out-GridView -Title "Select the V2 Standard Storage Account you want to scan for blob candidates ..." -PassThru)
$StorageAccountName = $StorageAccount.StorageAccountName
# Question 3 - Days Retention
write-host -nonewline "Enter the number of days retention you want to set, the tier will be set on any older blobs: " -ForegroundColor Green
[int]$DaysOld = read-host
# Setup the Storage Connection stuff
$StgAcct = Get-AzureRmStorageAccount | Where-Object {($_.StorageAccountName -eq $StorageAccountName) -and ($_.sku.tier -eq 'Standard') `
-and ($_.Kind -eq 'StorageV2' -or $_.Kind -eq 'BlobStorage')}
if ($StgAcct) {
$StgAcctKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $StgAcct.ResourceGroupName `
-Name $StgAcct.StorageAccountName).Value[0]
$StgAcctContext = New-AzureStorageContext -StorageAccountName $StgAcct.StorageAccountName `
-StorageAccountKey $StgAcctKey
$StorageContainers = Get-AzureStorageContainer -Context $StgAcctContext
# Cycle through all blobs in the storage account
$Blobs = @()
foreach($StorageContainer in $StorageContainers){
$Blobs += Get-AzureStorageBlob -Context $StgAcctContext -Container $StorageContainer.Name
}
# Date Logic
$UTCDate = (Get-Date).ToUniversalTime()
$RetentionDate = $UTCDate.AddDays(-$DaysOld)
$EarmarkedBlobs = $Blobs | Where-Object {$_.lastmodified.DateTime -le $RetentionDate}
# Change the Tier on the blobs
Foreach($Blob in $EarmarkedBlobs){
#Set tier of all blobs to desired tier
$blob.icloudblob.SetStandardBlobTier($StorageTier)
}
cls
Write-Host -ForegroundColor Green "`n`nDone... Complete. Check your Blog Tiers in the portal.`n"
} else {
Write-Error "Your selected Storage account cannot be used, most likely because it's `
not a V2 Storage account, a blob storage account or it's not a Standard Storage account"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment