Azure Storage

 1 Introduction
 2 Some simple examples
 3 Further examples

 

1 Introduction

 

2 Some simple examples

Example 1: Simple Creation of new storage

Set-StrictMode -Version "2.0"
Clear-Host
 
$Location = "North Europe"
$StorageName = "Store111abcd"
 
$StorageName = $StorageName.ToLower()
New-AzureStorageAccount -StorageAccountName $Storagename -Location $Location
#possible output

OperationDescription    OperationId                          OperationStatus
--------------------    -----------                          ---------------
New-AzureStorageAccount 4320d1d7-a1fa-10f9-ba15-4814d892d680 Succeeded      

 

Example 2: Simple Deletion of storage

Set-StrictMode -Version "2.0"
Clear-Host
 
$Location = "North Europe"
$StorageName = "Store111abcd"
 
$StorageName = $StorageName.ToLower()
Get-AzureStorageAccount $StorageName | Remove-AzureStorageAccount
#possible output

StorageAccountName OperationDescription       OperationId                          OperationStatus
------------------ --------------------       -----------                          ---------------
store111abcd       Remove-AzureStorageAccount 9cbe7d32-fe50-158d-8f6e-f63ce60e5bbe Succeeded 

 

3 Further examples

Example 1: Deletion of storage

Set-StrictMode -Version "2.0"
Clear-Host
 
Function Main{
  $Exceptions = @("portalvhdssk746zzcbsyd2","store11abc")
  $StorageAccountsToDelete = @(Get-AzureStorageAccount) | Where {$_.Label -NotIn $Exceptions}
 
  Delete-StorageAccount $StorageAccountsToDelete
} #End Function Main
 
Function Delete-StorageAccount{
  Param($AccountsToDelete)
  $AccountsToDelete  | Remove-AzureStorageAccount
}#Function Delete-StorageAccount
 
Main
#possible output

StorageAccountName                OperationDescription              OperationId          OperationStatus 
------------------                --------------------              -----------          ---------------     
store1ss                          Remove-AzureStorageAccount        00e33d87-f6f8-187b... Succeeded      
store1ss3                         Remove-AzureStorageAccount        085747de-edb1-11cf... Succeeded 

 

Example 2: Creating new storage if needed and possible

Set-StrictMode -Version "2.0"
Clear-Host
 
Function Main{
  $Location = "North Europe"
  $StorageName = "Store11abcd"
  #Storage account names must be between 3 and 24 characters 
  #in length and use numbers and lower-case letters only.
 
  New-Storage $StorageName $Location
    #$False: Name is invalid and cannot be used
    #<StorageName>: this name is valid 
}#End Function
 
Function New-Storage{
  Param($StorageName)
   
  $StorageName = $StorageName.ToLower()
  $YourOwnStorageAccounts = @(Get-AzureStorageAccount | Foreach{$_.Label})
 
  If ( (Test-AzureName –Storage $StorageName) -eq $False){
     #New Storage will be created
     New-AzureStorageAccount -StorageAccountName $Storagename -Location $Location
     $Subsname = (Get-AzureSubscription).Subscriptionname
     Set-AzureSubscription -SubscriptionName $Subsname -CurrentStorageAccountName $StorageName
     Write-Host "$StorageName Created"
     $Return = $StorageName
  }Else{
     #StorageName already exists 
     $Return = $False
  } #If
 
  If ( ($YourOwnStorageAccounts –Contains $StorageName) -eq $True){
     Write-Host "Storagename $Storagename is already in use by yourself and can be used."
     $Return = $StorageName
  } #if 
 
  If($Return -eq $False){
    Write-Host "Please use either an existing storagename of your own or an completely unused one in Azure."
    Write-Host "`"$Storagename`" is not available - use `"Test-AzureName –Storage `<Name>`" to find a free one"
    Write-Host "Exit script"
    Exit
   }
 
  Return $Return
}#End Function
 
Main
#possible output

store11abcd Created
OperationDescription    OperationId                          OperationStatus
--------------------    -----------                          ---------------
New-AzureStorageAccount d2457805d-f42e-1275-88a8-1d3a2d4c17d8 Succeeded      
store11abcd

Inside the function "New-Storage" it has been checked if the storage name is still free or busy or already exists in the used subscription.
If the name is still free then the storage will be created.