|
| 1 | +<# |
| 2 | +
|
| 3 | +.SYNOPSIS |
| 4 | +Return either the Primary or Seconday connection String to the Consig Storage account and Write to a VSTS variable. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | +Return either the Primary or Seconday connection String to the Consig Storage account and Write to a VSTS variable. |
| 8 | +
|
| 9 | +.PARAMETER Name |
| 10 | +The name of the Storage Account |
| 11 | +
|
| 12 | +.PARAMETER useSecondary |
| 13 | +Boolean Switch to Return Secondary String |
| 14 | +
|
| 15 | +.EXAMPLE |
| 16 | +.\Get-ConfigStorageKey.ps1 -Name stracc - -useSecondary $false |
| 17 | +
|
| 18 | +.EXAMPLE |
| 19 | +.\Get-ConfigStorageKey.ps1 -Name stracc -useSecondary $true |
| 20 | +
|
| 21 | +#> |
| 22 | + |
| 23 | +Param( |
| 24 | + [Parameter(Mandatory = $true)] |
| 25 | + [String]$Name, |
| 26 | + [Parameter(Mandatory = $false)] |
| 27 | + [bool]$useSecondary = $false) |
| 28 | + |
| 29 | + |
| 30 | +# --- Import Azure H |
| 31 | +Import-Module (Resolve-Path -Path $PSScriptRoot\..\Modules\Azure.psm1).Path |
| 32 | +Import-Module (Resolve-Path -Path $PSScriptRoot\..\Modules\Helpers.psm1).Path |
| 33 | + |
| 34 | +Write-Log -LogLevel Information -Message "Checking for existing Storage Account" |
| 35 | +# --- Check if storage account exists in our subscription |
| 36 | +$StorageAccount = Get-AzureRmResource -ResourceName $Name -ErrorAction SilentlyContinue |
| 37 | + |
| 38 | +# --- If the Storage Account doesn't exist, erorr |
| 39 | +if (!$StorageAccount) { |
| 40 | + Write-Log -LogLevel Information -Message "StorageAccount $Name Does not exist" |
| 41 | + } |
| 42 | + |
| 43 | +# --- If the storage account exists in this subscription get the key and set the env variable |
| 44 | +if ($StorageAccount -and !$useSecondary ) { |
| 45 | + #$ResourceGroup = $($StorageAccount.ResourceGroupName) |
| 46 | + $Key = (Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.ClassicStorage/storageAccounts" -ApiVersion "2016-11-01" -ResourceGroupName $($StorageAccount.ResourceGroupName) -ResourceName $($StorageAccount.Name) -force).primaryKey |
| 47 | + $ConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($Name);AccountKey=$($Key)" |
| 48 | + Write-Output ("##vso[task.setvariable variable=ConfigurationStorageConnectionString;]$($ConnectionString)") |
| 49 | +} else { |
| 50 | + #$ResourceGroup = $StorageAccount.ResourceGroupName |
| 51 | + $Key = (Invoke-AzureRmResourceAction -Action listKeys -ResourceType "Microsoft.ClassicStorage/storageAccounts" -ApiVersion "2016-11-01" -ResourceGroupName $($StorageAccount.ResourceGroupName) -ResourceName $($StorageAccount.Name) -force).secondaryKey |
| 52 | + $ConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($Name);AccountKey=$($Key)" |
| 53 | + Write-Output ("##vso[task.setvariable variable=ConfigurationStorageConnectionString;]$($ConnectionString)") |
| 54 | +} |
0 commit comments