How to set default storage Account for Azure RM Subscription - powershell

I am trying to set Azure Rm Subscription (Get-AzureRMSubscription) CurrentStorageAccount to a particular arm storage account (Get-AzureRmStorageAccount) and I am not able to find a cmdlet that does that.
With regular old azure cmdlets I am able to do following to set CurrentStorageAccount as
$subscription = Get-AzureSubscription
Set-AzureSubscription -SubscriptionName $subscription.SubscriptionName -CurrentStorageAccountName "somestorageaccount"
Get-AzureSubscription | select *
This set's it. But I cannot do this inside arm cmdlets.
Another thing that is confusing is that I am using the same subscription eg. Visual Studio Enterprise. And using both arm and regular cmdlets get-azuresubscription I get the same subscription but why is one showing -CurrentStorageAccount and another subscription not showing -CurrentStorageAccount.

To set the default RM subscription for the current session in PowerShell use
Get-AzureRmSubscription –SubscriptionName "MyFavSubscription" | Select-AzureRmSubscription
and to set the default RM storage context for the current session
Set-AzureRmCurrentStorageAccount –ResourceGroupName "MyFavResourceGroup" `
–StorageAccountName "MyFavStorageAccountName"

First, you must set your default subscription.
$SubscriptionName = "MyDefaultSubscription"
Select-AzureSubscription -SubscriptionName $SubscriptionName –Default
In other cases, you can set your default subscription location.
# For example, South Central US
$Location = "South Central US"
Then get your storage account name/s
$StorageAccountName = (Get-AzureStorageAccount)[0].label
Notice the number zero? It indicates the numbering of your storage. The numbering starts with 0. If you use the command Get-AzureStorageAccount, it will list all of your (classic) storage accounts. For that you can choose your desired storage.
Then lastly, set your default storage account.
Set-AzureSubscription -SubscriptionName $SubscriptionName -CurrentStorageAccountName $StorageAccountName

That commandlet is called Set-AzureRMCurrentStorageAccount.

Exactly as you said, set-azureRmCurrentStorageAccount -context $Ctx will set your default Storage account to context. I also can't find any articles to get out explanation on this. I think you can try to use Azure CLI to set your default Azure storage account in environment variables.

Related

VSTS Azure powershell : No default subscription has been designated

I'm trying to run some azure powershell commands as part of my Visual Studio Team Services build using Azure Resource Manager.
It gives me the following error:
No default subscription has been designated. Use Select-AzureSubscription -Default to set the default subscription.
The commands I'm trying to run:
$website = Get-AzureWebsite | where {$_.Name -eq 'my-website'}
Write-Output ("##vso[task.setvariable variable=DeployUrl;]$website.HostNames")
When I tried to run it locally, I had to call
Add-AzureAccount
Select-AzureRmSubscription -SubscriptionName "Visual Studio Premium with MSDN"
to get it working, but it is not possible in the VSTS build.
UPDATE:
I've configured it to use the azure classic mode instead of resource manager, at it works. I don't think that it is a feasible solution for production as azure classic mode is obsolete.
Since you are using Azure Resource Manager, please check the things below:
Make sure "Azure Resource Manager" service endpoint is added correctly.
Use "Get-AzureRmWebApp" command instead of "Get-AzureWebsite" command just as bmoore mentioned.
I have tested it at my side, it works correctly.
My PowerShell script:
$website = Get-AzureRmWebApp | where {$_.Name -eq 'eddieapp0930'}
Write-Host $website.HostNames
Run from "Azure PowerShell Script" task:
Thank you for your question.
If you are using service manager mode(classic mode), the correct cmdlet is:
Add-AzureAccount
Get-AzureSubscription -SubscriptionName “name” | Select-AzureSubscription
If you are using Resource Manager, the correct cmdlet is:
Login-AzureRmAccount
Get-AzureRmSubscription –SubscriptionName "name" | Select-AzureRmSubscription
or just use -SubscriptionId instead of -SubscriptionName.
More information about ASM and ARM, please refer to the link below:
https://azure.microsoft.com/en-us/documentation/articles/resource-manager-deployment-model/
If you still have questions, welcome to post back here. Thanks.

How do I authenticate Azure Powershell on Azure VM

I'm wanting to execute a Powershell script from an Azure VM to get its current public IP address (and to write this address to an evironment variable for an application to use).
My question is what the best way to authenticate the Azure Powershell environment is? On AWS credentials get 'baked' into an instance when it gets created. Does the equivalent happen with Azure Virtual Machines?
You can use a Management Certificate contained in your Publish Settings file and 'bake' it yourself
Import-AzurePublishSettingsFile –PublishSettingsFile C:\Store\my.publishsettings
If you already have a certificate for management, you can store it in your vm and use it in PS
# Get management certificate from personal store
$certificate = Get-Item cert:\\CurrentUser\My\$CertificateThumbprint
if ($certificate -eq $null) {
throw “Management certificate for $SubscriptionName was not found in the users personal certificate store. Check thumbprint or install certificate”
}
# Set subscription profile
Set-AzureSubscription -SubscriptionName $SubscriptionName -SubscriptionId $SubscriptionId -Certificate $certificate
# Select subscription as the current context
Select-AzureSubscription -SubscriptionName $SubscriptionName

How to manage multiple AzureRM accounts with Powershell

I tried to use Login-AzureRmAccount and Add-AzureRmAccount to login to my Azure Accounts. I have two of them, it was easy to add both of them via Add-AzureAccount and manage the active and default one using Select-Azuresubscription.
With the RM cmdlets every time I do Add-AzureRmAccount it overrides the previous authenticated one. This makes it hard for me to switch between a private and a company azure account.
Are there any solutions for that ?
I am using the PowerShell Gallery to update the Azure and AzureRM Modules and using the latest ones.
The official way is to do something like this
$profile1 = Login-AzureRmAccount
$profile2 = Login-AzureRmAccount
Select-AzureRmProfile -Profile $profile2
You can then save the profiles to disk using
Save-AzureRmProfile -Profile $profile1 -Path e:\ps\profile1.json
You can then load with
Select-AzureRmProfile -Path e:\ps\profile1.json
My personal approach though was to create a module that gave a cmdlet with profile1,profile2 etc as parameters. It would then download and decrypt credentials and feed them into Add-AzureRMAccount (this way I can use the same credential file from assorted locations)
Use Login-AzureRMAccout to login two accounts respectively. Then use Get-AzureRmSubscription to check the subscription info and note down the two TenantIds.
To switch between a private and a company azure account, you can specify the TenantId parameter using
$loadersubscription = Get-AzureRmSubscription -SubscriptionName $YourSubscriptionName -TenantId $YourAssociatedSubscriptionTenantId

manage azure resource manager storage powershell

I'm facing following issue
Switch-AzureMode AzureResourceManager
New-AzureStorageAccount -ResourceGroupName "XYZ" -Name "VmTemplateStorage" -Type "Standard_LRS"
# lists the account
Get-AzureStorageAccount
Set-AzureSubscription -SubscriptionName "ABC" -CurrentStorageAccountName -"VmTemplateStorage"
# now this outputs error saying: Storage account 'VmTemplateStorage' was not found.
Get-AzureStorageContainer
I'm aware that I can create storage account in "classic" mode. But then I'm unable to use it as a source for vm images deployed using resource manager.
This way however, I'm unable to manage the account and upload blobs using powershell.
Any ideas how to manage resource manager based storage accounts?
you can use the following new PowerShell commands to manage ARM (Azure Resource Manager) based storage accounts.
Note: You need to update your Azure PowerShell later versions which support this new feature such as the November 2015 release. Also, the Switch-AzureMode command is deprecated in the latest release.
New-AzureRmStorageAccount
Get-AzureRmStorageAccount
Set-AzureRmStorageAccount
Remove-AzureRmStorageAccount

Azure Powershell Select-AzureSubscription with subscription id

I am using Powershell Azure cmdlets to do some operation on each subscription I have.
However, all my subscriptions have the same name. So if I do an operation like:
$subs | ForEach-Object {
Select-AzureSubscription -Current -SubscriptionName $_.SubscriptionName
$services = Get-AzureService
Write-Output "$($services .Length) services under $($_.SubscriptionId) subscription"
}
it always works for the same subscription because the subscriptions only differ in subscription ID.
And the Select-AzureSubscription does not have a -SubscriptionId parameter.
Any ideas how can I find a workaround?
Not sure if this was added later, but as of Nov 2015 you can use -SubscriptionId
Write-Host "Selecting Azure subscription using id"
Select-AzureSubscription -SubscriptionId $subscriptionId
If you are an account administrator for your account, you can change the name of your subscription. This way when you or anyone else who has access to the subscription downloads publishsettings, the name will be set the way you want.
http://rickrainey.com/windows-azure-how-tos/how-to-change-the-name-of-your-windows-azure-subscription/
Edit the publishsettings file, giving each subscription a different name. Then re-import. At that point, you'll be able to easily access each one uniquely by name.