简体   繁体   中英

How to check storage account exists in Azure with Get-AzureStorageAccount

I am building a power shell script to automate the setup of a website environment in Azure. This web uses an account storage. I want to the script not to create the account storage if exists.

I thought that using Get-AzureStorageAccount this way may work but it does 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 issue is I don't know how to handle the return of Get-AzureStorageAccount .

Thank you very much in advance!

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 
}

You can use Test-AzureName for other services too, such as Cloud Services, WebSites, and ServiceBus. It returns True if it exists, False otherwise.

Get-AzureRmStorageAccountNameAvailability -Name "accountname"

Try this:

$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      
}

Test-AzureName didn't work with our build agents and we already had a try/catch in code so a second one would require building it out as a function. I opted for that standard get and check if null, use -ErrorAction Ignore to stop it throwing an exception

# 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   
}

@Rick Rainey's solution works if you're logged in using Add-AzureAccount . However, Azure and powershell have a conflicting and confusing suite of login accounts (Windows Live versus AD) and login mechanisms (Classic: Add-AzureAccount ; Resource manager: Login-AzureRmAccount ). Some Azure powershell cmdlets require a specific login; further, some require a specific account type!

To clear through this thicket of complicated, undocumented, and confusing permission issues, we always use an AD account, logging in via Login-AzureRmAccount . We also use Azure resource manager (ARM) resources and cmdlets, following Microsoft's movement to ARM as its recommended and strategic approach. However, @RIck's solution is one which the ARM login doesn't work with. :-( So you need another approach, which is @Darren's (for storage). However, for a generic replacement for Test-AzureName I'd suggest Find-AzureRmResource . In the case of storage

$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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM