How to verify storage account in Azure using Get-AzureStorageAccount

I am creating a power shell script to automate the setup of a website environment in Azure. This network uses account storage. I want the script not to create an account store, if one exists.

I thought using Get-AzureStorageAccountthis might work, but it is not:

Write-Verbose "[Start] creating $Name storage account $Location location"

$storageAcct = Get-AzureStorageAccount –StorageAccountName $Name
if (!$storageAcct)
{   
    $storageAcct = New-AzureStorageAccount -StorageAccountName $Name -Location $Location -Verbose
    if ($storageAcct)
    {
        Write-Verbose "[Finish] creating $Name storage account in $Location location"
    }
    else
    {
        throw "Failed to create a Windows Azure storage account. Failure in New-AzureStorage.ps1"
    }
}
else
{
    Write-Verbose "$Name storage account in $Location location already exists, skipping creation"
}

The problem is that I do not know how to handle the return Get-AzureStorageAccount.

Thank you in advance!

+4
source share
5 answers

I would suggest using the Test-AzureName cmdlet to determine if it exists. So, something like this.

if (!(Test-AzureName -Storage $Name))
{  
    Write-Host "Creating Storage Account $Name"
    New-AzureStorageAccount -StorageAccountName $Name -Location $Location 
}

Test-AzureName , Cloud Services, WebSites ServiceBus. True, , False.

+15

:

$Name = "myStorageAccount"
$Location = "myLocation"

Write-Host "[Start] creating $Name storage account $Location location"
try{
    Get-AzureStorageAccount –StorageAccountName $Name -ErrorAction Stop | Out-Null
    Write-Host "$Name storage account in $Location location already exists, skipping creation"
    }
catch{
    Write-Host "[Finish] creating $Name storage account in $Location location"
    New-AzureStorageAccount -StorageAccountName $Name -Location $Location -Verbose      
}
+3
Get-AzureRmStorageAccountNameAvailability -Name "accountname"
+3

Test-AzureName , try/catch, . get , null, -ErrorAction Ignore, , .

# Check for storage account and create if not found    
$StorageAccount = Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $StorageAccountRG -ErrorAction Ignore
if ($StorageAccount -eq $null)  
{    
    New-AzureRmStorageAccount -Location "West Europe" -Name $StorageAccountName -ResourceGroupName $StorageAccountRG -SkuName Standard_LRS -Kind Storage     
    $StorageAccount = Get-AzureRmStorageAccount -Name $StorageAccountName -ResourceGroupName $StorageAccountRG   
}
+1

@Rick Rainey , Add-AzureAccount. Azure powershell (Windows Live AD) (Classic: Add-AzureAccount; Resource manager: Login-AzureRmAccount). Azure powershell ; , !

, , AD, Login-AzureRmAccount. Azure (ARM), Microsoft ARM . , @RIck - , ARM.:-( , @Darren ( ). Test-AzureName Find-AzureRmResource.

$StorageObject = Find-AzureRmResource -ResourceType "Microsoft.Storage/storageAccounts" | Where-Object {$_.Name -eq $storageName}        
if  ( !$StorageObject ) {
    $storageLocation = (Get-AzureRmResourceGroup -ResourceGroupName $resourceGroup).Location
    $storageType = "Standard_LRS"
    New-AzureRmStorageAccount -ResourceGroupName $resourceGroup  -Name $storageName -Location $storageLocation -Type $storageType
}
+1

Source: https://habr.com/ru/post/1545664/


All Articles