Tuesday 7 January 2020

Azure Useful PowerShel Scripts -1

Here are the few very useful Azure PowerShell-CLI short commands for basic checks:

Select Subscription:
Get-AzureRmSubscription
Select-AzureRmSubscription -SubscriptionId "12y127128y172kajsnkdaj"
List RG:
Get-AzureRmResourceGroup | SELECT ResourceGroupName,Location
List of Storage Account:
Get-AzureRmStorageAccount | Select StorageAccountName, Location
Get Storage Account Key:
Get-AzureRmStorageAccountKey -ResourceGroupName "RGName" -AccountName "Storage_AccName"
List Containers:
$context = New-AzureStorageContext -StorageAccountName "Storage_AccName" `
-StorageAccountKey "XXXXXXXXXXXX"
List of all Blobs\Files:

Get-AzureStorageBlob -Container 'Container_Name' -Context $context
 | Select-Object @{name="Name"; 
expression={"https://xyz.blob.core.windows.net/sqldbbackups1/"+$_.Name}}
 | Where-Object { $_.Name -like '*.bak*'}

OR to use in restore command:

Get-AzureStorageBlob -Container 'Container_Name' -Context $context
 | Select-Object @{name="Name"; 
expression={"URL = 'https://xyz.blob.core.windows.net/sqldbbackups1/"+$_.Name + "',"}}
 | Where-Object { $_.Name -like '*.bak*'} | Format-List *

Change/replace $ sign to %24 to convert into URL:

Get-AzureStorageBlob -Container 'Container_Name' -Context $context
 | Select-Object @{name="Name"; 
expression={"URL = 'https://xyz.blob.core.windows.net/sqldbbackups1/"+$_.Name.replace('$','%24')
 + "',"}}
 | Where-Object { $_.Name -like '*.bak*'} | Format-List *
Real time used Sample Scripts :
Get the list of files with last modified date:
Get-AzureStorageContainer -Context $context | SELECT Name, Lastmodified

Another example, Check if files are older than 7 days on Storage blob:

$lastdate = Get-AzureStorageBlob -Container "Cont_Name" -Context $context  `
| SELECT Lastmodified | sort @{expression="LastModified";Descending=$false} | `
SELECT -First 1 | ft -HideTableHeaders | Out-String
$checkdate = Get-Date -date $(Get-Date).AddDays(-7) -Format "MM/dd/yyyy HH:mm K"

if($lastdate -le $checkdate) {
$fromaddress = "donotreply@xyz.com"
$toaddress = "amit@xyz.com"
$Subject = "Action Required : No Truncation "
$body = "Backup files are not getting truncated!!"
$smtpserver = "smtpmail.xyz.com"
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.Subject = $Subject
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
Write-Output "Alert triggered!!"}
else{
     Write-Output "All Good!!"
}

Count Total Files and Size Azure blob: 

$resourceGroup = "RGName"
$storageAccountName = "Storage_AccName"
$containerName = "containerName"

# get a reference to the storage account and the context
$storageAccount = Get-AzureRmStorageAccount -ResourceGroupName `
$resourceGroup -Name $storageAccountName
$context = $storageAccount.Context

# get a list of all of the blobs in the container 
$listOfBLobs = Get-AzureStorageBlob -Container $ContainerName -Context $context 

# zero out our total
$length = 0

# this loops through the list of blobs and retrieves the length for each blob
#   and adds it to the total
$listOfBlobs | ForEach-Object {$length = $length + $_.Length}

$count=$listOfBlobs | select Name, Length
$length= (($length)/1073741824)

# output the blobs and their sizes and the total 
Write-Host "List of Blobs and their size (length)"
Write-Host " " 
Write-Host " "
Write-Host "Total Files Count = " $count.Count
Write-Host "Total Size in GiB = " $length 

No comments:

Post a Comment