Working with Azure Storage in Powershell

Here’s some quick snips to help work with Azure Storage. Everything from creating the storage account, to manipulating it with files.


#Create Azure storage and manipulate it.

#update with the name if your subscription
$subscriptionName = "Pay-As-You-Go"

#Give a name to your new storage account.  must be lowercase.  Can test this with powershell as well
$storageAccountName = "teststorage001"

#Choose location 
$location = "west us"

#Give a name to your new container
$containerName = "pictures"

#have an image file and a source directory on your local computer
$imageToUpload - "C:\temp\myimage.jpg"

#A destination directory in your local computer
$destinationFolder = "C:\temp"

# Resource Group
$resGroup = "Storage"

#Add your Azure account to the local powershell environment
Add-AzureRMAccount

#Create a resource group
New-AzureRMResourceGroup -Name $resGroup -Location $location

#Create a new storage account
New-AzureRMStorageAccount -StorageAccountName $StorageAccountName -Location $location -ResourceGroupName $resGroup -SkuName Standard_LRS

#Set a current storage account
Set-AzureRMCurrentStorageAccount -ResourceGroupName $resGroup -Name $storageAccountName

#Create a new container  (container name must be lowercase)
New-AzureStorageContainer -Name $containerName -Permission Off    
##Permission Off, Blob or Container  

#Upload a blob into a container
Set-AzureStorageBlobContent -Container $containerName -File $imageToUpload

#list all blobs in a container
Get-AzureStorageBlob -Container $ContainerName

#Download blobs from the container
#Get a reference to a list of all blobs in a container
$blobs = Get-AzureStorageBlob -Container $containerName

#Create the destination directory
New-Item -Path $destinationFolder -ItemType Directory -Force

#Download blobs into the local destination directory
$blobs | Get-AzureStorageBlobContent -Destination $destinationFolder

#end

#Azure Storage Account Access Keys

#Login
Login-AzureRMAccount

#Set Variables for resource group and storage account
$rg = 'Storage-Security'
$sa = 'secstorge001'

#recycle storage keys - get them, then reset key1
$curKeys = Get-AzureRMStorageAccountKey -ResourceGroupName $rg -Name $sa
$NewKeys = New-AzureRMStorageAccountKey -ResourceGroupName $rg -Name $sa -KeyName key1

#List keys.  $curKeys
#List New Keys.  $NewKeys.Keys

#Set Storage Account
Set-AzureRmCurrentStorageAccount -ResourceGroupName $rg -Name $sa

#Generate SAS Access URI  
$Token = New-AzureStorageTableSASToken -Name 'MyToken' -Permission 'raud' -FullUri 

#Note- this is for a table.  If Queue - New-AzureStorageQueueSASToken
#List token.  $Token

####Can include paramaters such as Protocol, IPAddressOrRange,StartTime,ExpiryTime...



Leave a Reply

Your email address will not be published. Required fields are marked *