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.
- $UserName=Learn-Host `n‘Please Enter the Person Title’
- $password=Learn-Host `n‘Please Enter the Password’
- $EncPassword = ConvertTo-SecureString $password -AsPlainText -Power
- $Credential = New-Object -TypeName System.Administration.Automation.PSCredential -ArgumentList $UserName, $EncPassword
- $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.
- $resourceGroup = “existingresourcegroup”
- $storageAccountName = “existingstorageaccount”
- $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup `
- -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.
- # Get listing of places and choose one.
- Get-AzLocation | choose Location
- $location = “eastus”
- # Set the title of the storage account and the SKU title.
- $storageAccountName = “testpshstorage”
- $skuName = “Standard_LRS”
- $resourceGroup=“<Use current useful resource group title>”
- # Create the storage account.
- $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `
- -Title $storageAccountName `
- -Location $location `
- -SkuName $skuName
- # Retrieve the context.
- $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”
.
- $StorageAccountName =Learn-Host `n’Please Enter the Storage account title’
- $ResourceGroupName = Learn-Host `n‘Please Enter the Useful resource group title()’
- $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Title $StorageAccountName
- If ($StorageaccountnameExist.StorageAccountName -ne $null) {
- $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore
- $ctx = $StorageaccountnameExist.Context
- $TableName =Learn-Host `n‘Please Enter the Storage Desk title’
- $storageTable = Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore
- if($storageTable.Title -eq $null){
- $createTable=New-AzStorageTable –Title $TableName –Context $ctx
- #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.
- # Add one entry
- $CloudTable = (Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore).CloudTable
- Add-AzTableRow `
- -table $CloudTable `
- -partitionKey “1” `
- -rowKey (“1”) -property @{“ColumnName1”=“Testing”;“ColumnName2”=“1”}
- }
- }
Full script right here,
- Create Storage account
- Create Desk (Utilizing current stroage account or New stroage account)
Add Desk Entities
- #————-Azure Storage account & Desk Creation———
- perform AzureStorageTableCreation{
- #========================Decelared Varibales==========================
- $UserName=Learn-Host `n‘Please Enter the Person Title’
- $password=Learn-Host `n‘Please Enter the Password’
- $EncPassword = ConvertTo-SecureString $password -AsPlainText -Power
- $StorageAccountName = “”
- $TableName=“”
- $StorageAccountAccessKey=“”
- #====================================================================
- If($UserName.size -gt 0 -and $password.size -gt 0){
- $Credential = New-Object -TypeName System.Administration.Automation.PSCredential -ArgumentList $UserName, $EncPassword
- $ctx= Join-AzAccount -Credential $Credential
- if ($ctx -ne $null) {
- #==================Storage account particulars=================================================================
- Write-Host `n“Do you need to create new Storage Account Title or use current one?” -ForegroundColor Cyan
- DO{
- $StorageInp = Learn-Host “Present legitimate enter[‘Y’- New Stroage Account Name; ‘N’ – Use existing]”
- $StorageAccountName =Learn-Host `n’Please Enter the Storage account title(Storage account title should be
- between 3 and 24 characters in size and use numbers and lower-case letters solely)’
- if($StorageInp -eq ‘N’){
- $ResourceGroupName = Learn-Host `n‘Please Enter the Useful resource group title’
- $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Title $StorageAccountName
- if ($StorageaccountnameExist.StorageAccountName -ne $null) {
- $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore
- $ctx = $StorageaccountnameExist.Context
- #=================accessing storage Desk particulars===============================================================================
- $TableName =Learn-Host `n‘Please Enter the Storage Desk title’
- $storageTable = Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore
- if($storageTable.Title -eq $null){
- #Take away-AzStorageTable –Title $TableName –Context $ctx
- $createTable=New-AzStorageTable –Title $TableName –Context $ctx
- # Add one entry
- $CloudTable = (Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore).CloudTable
- Add-AzTableRow `
- -table $CloudTable `
- -partitionKey “1” `
- -rowKey (“1”) -property @{“ColumName1”=“Testing”;“ColumName2”=“1”}
- Write-Host `n“Storage Desk and Fields Inserted Sucessfully” -ForegroundColor Cyan
- }
- else
- {
- Write-Host `n“Storage Desk already exist in azure storage” -ForegroundColor Cyan
- }
- #================Finish stroage Desk particulars======================================================================================
- }
- }
- else{
- #Creation New storage account
- attempt
- {
- $ResourceGroupName = Learn-Host `n‘Please Enter the Useful resource group title’
- $Location = Learn-Host `n‘Please Enter the Location’
- $SkuName = Learn-Host `n‘Please Enter the Sku Title’
- $StorageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName `
- -Title $StorageAccountName `
- -Location $Location `
- -SkuName $SkuName
- $StorageaccountnameExist= Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Title $StorageAccountName -ErrorAction Ignore
- if ($StorageaccountnameExist.StorageAccountName -ne $null) {
- Write-Host `n“New Storage account created Sucessfully” -ForegroundColor Cyan
- $StorageAccountAccessKey=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -AccountName $StorageaccountnameExist.StorageAccountName -ErrorAction Ignore
- $ctx = $StorageaccountnameExist.Context
- #=================accessing storage Desk particulars===============================================================================
- $TableName =Learn-Host `n‘Please Enter the Storage Desk title’
- $storageTable = Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore
- if($storageTable.Title -eq $null){
- #Take away-AzStorageTable –Title $TableName –Context $ctx
- $createTable=New-AzStorageTable –Title $TableName –Context $ctx
- # Add one entry
- $CloudTable = (Get-AzStorageTable –Title $TableName –Context $ctx -ErrorAction Ignore).CloudTable
- Add-AzTableRow `
- -table $CloudTable `
- -partitionKey “1” `
- -rowKey (“1”) -property @{“ColumName1”=“Testing”;“ColumName2”=“1”}
- Write-Host `n“Storage Desk and Fields Inserted Sucessfully” -ForegroundColor Cyan
- }
- else
- {
- Write-Host `n“Storage Desk already exist in azure storage” -ForegroundColor Cyan
- }
- #================Finish stroage Desk particulars======================================================================================
- }
- }
- catch
- {
- }
- }
- }Till($StorageInp -eq ‘Y’ -or $StorageInp -eq ‘N’)
- #==================Finish Storage account =================================================================
- }
- else
- {
- Write-Host `n“Invalid credentials” -ForegroundColor Pink
- }
- }
- }
- #—————————————————–Script Finish———————————————————
AzureStorageTableCreation