Azure

How To Create Azure Storage Account And Storage Desk Utilizing PowerShell

Azure PowerShell is used to create and handle Azure Storage account & Storage Desk from the PowerShell command line or in scripts

 

This how-to article covers frequent operations utilizing the administration airplane cmdlets to handle storage accounts. You discover ways to,

  • Create a storage account utilizing current useful resource group title
  • Create a desk (Utilizing Current Storage account or New account Title)
  • Add desk entities

Check in to Azure

 

Check in to your Azure subscription with the Add-AzAccount command and comply with the on-screen instructions.

  1. $UserName=Learn-Host `n‘Please Enter the Person Title’  
  2. $password=Learn-Host `n‘Please Enter the Password’  
  3. $EncPassword = ConvertTo-SecureString $password -AsPlainText -Power  
  4. $Credential = New-Object -TypeName System.Administration.Automation.PSCredential -ArgumentList $UserName, $EncPassword  
  5. $ctx= Join-AzAccount -Credential $Credential  

Use an current storage account

 

To retrieve an current storage account, you want the title of the useful resource group and the title of the storage account. Set the variables for these two fields.

  1. $resourceGroup = “existingresourcegroup”  
  2. $storageAccountName = “existingstorageaccount”  
  3. $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup `  
  4. -Title $storageAccountName  

Create a storage account

 

The next script reveals how one can create a general-purpose storage account utilizing New-AzStorageAccount. After you create the account, retrieve its context, which can be utilized in subsequent instructions moderately than specifying the authentication with every name.

  1. # Get listing of places and choose one.  
  2. Get-AzLocation | choose Location  
  3. $location = “eastus”  
  4. # Set the title of the storage account and the SKU title.  
  5. $storageAccountName = “testpshstorage”  
  6. $skuName = “Standard_LRS”  
  7. $resourceGroup=“<Use current useful resource group title>”   
  8. # Create the storage account.  
  9. $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `  
  10. -Title $storageAccountName `  
  11. -Location $location `  
  12. -SkuName $skuName  
  13. # Retrieve the context.  
  14. $ctx = $storageAccount.Context   

The script makes use of the next PowerShell cmdlets,

  • G Get-AzLocation — retrieves an inventory of the legitimate places. The instance makes use of eastus for location.
  • N New-AzStorageAccount — creates the storage account. The instance makes use of testpshstorage.

The SKU title signifies the kind of replication for the storage account, corresponding to LRS (Domestically Redundant Storage).

 

Create a brand new desk

 

To create a desk, use the New-AzStorageTable cmdlet. On this instance, the desk is known as “TestTable”.

  1. $StorageAccountName =Learn-Host `n’Please Enter the Storage account title’  
  2. $ResourceGroupName = Learn-Host `n‘Please Enter the Useful resource group title()’  
  3. $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Title $StorageAccountName  
  4. If ($StorageaccountnameExist.StorageAccountName -ne $null) {  
  5.    $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore  
  6.    $ctx = $StorageaccountnameExist.Context  
  7.    $TableName =Learn-Host `n‘Please Enter the Storage Desk title’  
  8.    $storageTable = Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore  
  9.    if($storageTable.Title -eq $null){  
  10.       $createTable=New-AzStorageTable –Title $TableName –Context $ctx  
  11.       #as soon as Desk is created efficiently, Add desk entities  

Utilization of CloudTable is necessary when working with AzTable PowerShell module. Name the Get-AzTableTable command to get the reference to this object. 

  1. # Add one entry  
  2. $CloudTable = (Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore).CloudTable  
  3.    Add-AzTableRow `  
  4.    -table $CloudTable `  
  5.    -partitionKey “1” `  
  6.    -rowKey (“1”) -property @{“ColumnName1”=“Testing”;“ColumnName2”=“1”}  
  7.   }  
  8. }  

Full script right here,

  1. Create Storage account 
  2. Create Desk (Utilizing current stroage account or New stroage account) 

Add Desk Entities 

  1. #————-Azure Storage account & Desk Creation———  
  2. perform AzureStorageTableCreation{  
  3. #========================Decelared Varibales==========================  
  4. $UserName=Learn-Host `n‘Please Enter the Person Title’  
  5. $password=Learn-Host `n‘Please Enter the Password’  
  6. $EncPassword = ConvertTo-SecureString $password -AsPlainText -Power  
  7. $StorageAccountName = “”  
  8. $TableName=“”  
  9. $StorageAccountAccessKey=“”  
  10. #====================================================================  
  11. If($UserName.size -gt 0 -and $password.size -gt 0){  
  12.    $Credential = New-Object -TypeName System.Administration.Automation.PSCredential -ArgumentList $UserName, $EncPassword  
  13.    $ctx= Join-AzAccount -Credential $Credential  
  14.    if ($ctx -ne $null) {  
  15.       #==================Storage account particulars=================================================================  
  16.       Write-Host `n“Do you need to create new Storage Account Title or use current one?” -ForegroundColor Cyan  
  17.       DO{  
  18.          $StorageInp = Learn-Host “Present legitimate enter[‘Y’- New Stroage Account Name; ‘N’ – Use existing]”  
  19.          $StorageAccountName =Learn-Host `n’Please Enter the Storage account title(Storage account title should be  
  20.          between 3 and 24 characters in size and use numbers and lower-case letters solely)’  
  21.          if($StorageInp -eq ‘N’){  
  22.             $ResourceGroupName = Learn-Host `n‘Please Enter the Useful resource group title’  
  23.             $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Title $StorageAccountName  
  24.             if ($StorageaccountnameExist.StorageAccountName -ne $null) {  
  25.                $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore  
  26.                $ctx = $StorageaccountnameExist.Context  
  27.                #=================accessing storage Desk particulars===============================================================================  
  28.                $TableName =Learn-Host `n‘Please Enter the Storage Desk title’  
  29.                $storageTable = Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore  
  30.    if($storageTable.Title -eq $null){  
  31.       #Take away-AzStorageTable –Title $TableName –Context $ctx  
  32.       $createTable=New-AzStorageTable –Title $TableName –Context $ctx  
  33.       # Add one entry  
  34.       $CloudTable = (Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore).CloudTable  
  35.       Add-AzTableRow `  
  36.       -table $CloudTable `  
  37.       -partitionKey “1” `  
  38.       -rowKey (“1”) -property @{“ColumName1”=“Testing”;“ColumName2”=“1”}  
  39.       Write-Host `n“Storage Desk and Fields Inserted Sucessfully” -ForegroundColor Cyan  
  40.       }  
  41.    else  
  42.    {  
  43.       Write-Host `n“Storage Desk already exist in azure storage” -ForegroundColor Cyan  
  44.    }  
  45.    #================Finish stroage Desk particulars======================================================================================  
  46.    }  
  47. }  
  48. else{  
  49.    #Creation New storage account  
  50.    attempt  
  51.    {  
  52.       $ResourceGroupName = Learn-Host `n‘Please Enter the Useful resource group title’  
  53.       $Location = Learn-Host `n‘Please Enter the Location’  
  54.       $SkuName = Learn-Host `n‘Please Enter the Sku Title’  
  55.       $StorageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName `  
  56.       -Title $StorageAccountName `  
  57.       -Location $Location `  
  58.       -SkuName $SkuName  
  59.       $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Title $StorageAccountName -ErrorAction Ignore  
  60.       if ($StorageaccountnameExist.StorageAccountName -ne $null) {  
  61.          Write-Host `n“New Storage account created Sucessfully” -ForegroundColor Cyan  
  62.          $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore  
  63.          $ctx = $StorageaccountnameExist.Context  
  64.          #=================accessing storage Desk particulars===============================================================================  
  65.          $TableName =Learn-Host `n‘Please Enter the Storage Desk title’  
  66.          $storageTable = Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore  
  67.          if($storageTable.Title -eq $null){  
  68.             #Take away-AzStorageTable –Title $TableName –Context $ctx  
  69.             $createTable=New-AzStorageTable –Title $TableName –Context $ctx  
  70.             # Add one entry  
  71.             $CloudTable = (Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore).CloudTable  
  72.             Add-AzTableRow `  
  73.             -table $CloudTable `  
  74.             -partitionKey “1” `  
  75.             -rowKey (“1”) -property @{“ColumName1”=“Testing”;“ColumName2”=“1”}  
  76.             Write-Host `n“Storage Desk and Fields Inserted Sucessfully” -ForegroundColor Cyan  
  77.             }  
  78.          else  
  79.          {  
  80.             Write-Host `n“Storage Desk already exist in azure storage” -ForegroundColor Cyan  
  81.          }  
  82.          #================Finish stroage Desk particulars======================================================================================  
  83.        }  
  84.       }  
  85.    catch  
  86.       {  
  87.       }  
  88.       }  
  89.    }Till($StorageInp -eq ‘Y’ -or $StorageInp -eq ‘N’)  
  90.    #==================Finish Storage account =================================================================  
  91.    }  
  92.    else  
  93.    {  
  94.       Write-Host `n“Invalid credentials” -ForegroundColor Pink  
  95.    }  
  96.  }  
  97. }  
  98. #—————————————————–Script Finish———————————————————  

AzureStorageTableCreation

Show More

Related Articles

Leave a Reply

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

Back to top button