Deploying web role in Azure throws exception on CurrentAccountStorageName - powershell

I want to deploy a web role to Azure using the PowerShell CmdLets.
My script is as follows:
$subscription = "<name-of-subscription>"
$service = "<name-of-cloudservice>"
$slot = "staging"
$package = "path\to\package.cspkg"
$configuration = path\to\config.cscfg"
$deploymentLabel = "Deploy to $service"
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1"
Import-AzurePublishSettingsFile "C:\path-to.publishsettings"
Set-AzureSubscription -CurrentStorageAccount $service -SubscriptionName $subscription
# some more stuff to check whether to upgrade or to create ...
Set-AzureDeployment -Upgrade -Slot $slot -Package $package -Configuration $configuration -label $deploymentLabel -ServiceName $service -Force
When I execute this it throws an error:
Exception: The subscription named <name-of-subscription> already exists.
I figured that since I'm importing my publishsettings-file already I could get rid of Set-AzureSubscription. However, once I do that I get the next error:
Exception: CurrentStorageAccountName is not set.
Use Set-AzureSubscription subname -CurrentStorageAccountName storageaccount to set it.
This is the line that gave me the error in the first place, so I'm not sure how I need to set the storageaccountname without causing an error.
I also ran a little test:
Import-AzurePublishSettingsFile "C:\path-to.publishsettings"
Get-AzureSubscription | Format-Table
Once I do this I get the following output (reformatted):
SubscriptionName: <name-of-subscription>
SubscriptionId: 123456789-123...
ServiceEndpoint: https://man....
ActiveDirectoryEndpoint:
ActiveDirectoryTenantId:
IsDefault: True
Certificate [Subject]
CurrentStorageAccountName
CurrentCloudStorageAccount
ActiveDirectoryUserId
As you can see, the CurrentStorageAccountName is empty, but I don't know how I can set it to a correct value.
I looked up some other scripts, but they all seem to be having this sequence of importing, then setting. Any idea why this is not working and how to solve it?

You are trying to set CurrentStorageAccount to the name of your cloudservice, but you should be setting it to the name of your blob storage account. Get the list of you storage accounts
PS U:\>Get-AzureStorageAccount |select StorageAccountName
StorageAccountName
------------------
portalvhdsgsomething
storage1
storage2
storage3
then run your previously failing line but with the name of your storage account ie.
Set-AzureSubscription -CurrentStorageAccount storage2 -SubscriptionName $subscription
You can confirm your changes with
PS U:\>Get-AzureSubscription | select CurrentStorageAccount
CurrentStorageAccount : storage2

You need to use -CurrentStorageAccountName as the parameter of Set-AzureSubscription.

For Set-AzureSubscription the MSDN doc shows the -SubscriptionName parameter as parameter 1, not as named. The error message's suggested fix even implied that you needed to do Set-AzureSubscription <service> -CurrentStorageAccount <storage solution> Perhaps simply moving that will solve it?
Set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccount $service
Reference: http://msdn.microsoft.com/en-us/library/dn495189.aspx

I have found out the reason. It appears that you cannot include the subscription name as a named parameter. Changing this line:
Set-AzureSubscription -CurrentStorageAccount $service -SubscriptionName $subscription
to this line did the trick
Set-AzureSubscription $subscription -CurrentStorageAccount $service

Related

Run cosmosdb commands from powershell

I am trying to connect to azure cosmosdb from my local machine via powershell but every command I tried to run it returns the "Argument passed in is not serializable."
Here are a few of my commands,
Get-AzCosmosDBAccount -ResourceGroupName "cosmosbackup"
Invoke-AzCosmosDBSqlDatabaseThroughputMigration -ResourceGroupName "cosmosbackup" -AccountName "liabilitydata" -Name liability
New-AzCosmosDBSqlContainer -AccountName "liabilitydata"-DatabaseName "dailyliability"-ResourceGroupName "cosmosbackup"-Name schemes -PartitionKeyPath /Id -PartitionKeyKind Hash
Get-AzCosmosDBSqlContainer `
-ResourceGroupName "cosmosbackup" `
-AccountName "liabilitydata" `
-DatabaseName "dailyliability"
All of them fail for the same reason Argument passed in is not serializable.
Am I missing something? Please help
The issue here is that you need to set the context for running the script,
Step 1 : Connect with your Azure account
Connect-AzAccount
Step 2 : Pass the resource group and the cosmosdb account name as follows,
Get-AzCosmosDBAccount -ResourceGroupName cosmosbackup

Azure PowerShell automation "no default subscritpion has been designated"

I am getting the following error. I am in fact setting the default subscription name.
4/27/2015 10:28:28 AM, Error: Get-AzureVM : No default subscription
has been designated. Use Select-AzureSubscription -Default
to set the default subscription. At test:9 char:9
+
+ CategoryInfo : CloseError: (:) [Get-AzureVM], ApplicationException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand
Here is my code:
workflow test
{
# Initial set up
$Cred = Get-AutomationPSCredential -Name "******"
Add-AzureAccount -Credential $Cred
Select-AzureSubscription -Default -SubscriptionName 'Beebunny'
$vmName = "MyMachineName"
Get-AzureVM -servicename $vmName
Write-output "All done."
}
If I try Select-AzureSubscription -Default 'SubscriptionName' it throws an error saying the syntax is invalid.
Edit: I have also tried Select-AzureSubscription -SubscriptionName 'SubscriptionName' without the Default flag.
Funny thing is that if I run this in AzurePS directly from Windows, it runs just fine. I am about 95% sure this is an Azure bug but wanted to get a second opinion first.
What version of the Azure module do you have loaded? Are you using the default module provided by the Automation service? Also, have you imported any other modules to this subscription?
Try creating a clean runbook with the following code, replacing the credential and subscription with the proper names. Can you get the credential and authenticate successfully?
workflow Test-GetVM
{
$Cred = Get-AutomationPSCredential -Name 'AdAzureCred'
if(!$Cred) {
Throw "Could not find an Automation Credential Asset named. Make sure you have created one in this Automation Account."
}
$Account = Add-AzureAccount -Credential $Cred
if(!$Account) {
Throw "Could not authenticate to Azure. Make sure the user name and password are correct."
}
Select-AzureSubscription -SubscriptionName "Visual Studio Ultimate with MSDN"
Get-AzureVM
}
UPDATE: Do you have the Resource Manager module loaded to the subscription as well?
I had the same problem and the solution was execute Add-AzureAccount, do the login process requested and once done all was working.

How to run Azure automation runbook in power shell

I am trying to run 'Connect-AzureVM' by importing it from the gallery. But no service or VM are created as I get these errors in job 'History'. The credential 'xyz' and subscription name 'ABC' both exist; I don't know why it's throwing an error.
workflow m1
{
$Cred = Get-AutomationPSCredential -Name "xyz"
Add-AzureAccount -Credential $Cred
InlineScript {
Select-AzureSubscription -SubscriptionName "ABC"
Get-AzureVM | select InstanceName
}
}
I am getting these errors:
Error: System.Management.Automation.ParameterBindingValidationException: Cannot bind argument to parameter 'Credential' because it is null
Error: The subscription named 'ABC' cannot be found. Use Set-AzureSubscription to initialize the subscription data.
It appears $Cred is null in the above runbook. Are you sure you have created the credential "xyz" as an Automation credential asset in the automation account where this runbook is running?
If you add the lines:
$CredIsNull = $Cred -eq $Null
Write-Output $Cred
Write-Output $CredIsNull
When you run the runbook, what does it output for $Cred and $CredIsNull?

Azure Powershell to stop VM's in a subscription

I am new to Powershell, and to be specific entirely new to Azure Powershell. I need to create a powershell script to switch off all the VM's found on a subscription. I guess this needs to be done with the help of management certificate. But not sure where to start. I just done some simple lines of code to list all VM's as shown below:
Add-AzureAccount -Environment "AzureCloud"
$subscription = "my-sub-scri-ption"
Select-AzureSubscription -Current "SubName"
Set-AzureSubscription -SubscriptionName "SubName"
Get-AzureVM -ServiceName "VM1"
The output recieved is "Get-AzureVM : Value cannot be null.
Parameter name: subscriptionId".
Can someone please help me in this regard?
**EDIT:**
The powershell script which I am using is given below:
Add-AzureAccount -Environment "AzureCloud"
Set-AzureSubscription -SubscriptionName "My Subs"
$serviceName = "Service01"
$vmName = "Service01"
Get-AzureVM | Stop-AzureVM -Force
Though while running it shows script execution is successful, I can see still that vm is powered on. Please note that the serviceName and vmName is same in my case. Anything wrong here in my code?
**Re-Edit**
Executed code:
Add-AzureAccount -Environment "AzureCloud"
Set-AzureSubscription -SubscriptionName "My Subs"
$serviceName = "Service01"
$vmName = "Service01"
Get-AzureVM
Error for the above code:
Get-AzureVM : Value cannot be null.
Parameter name: subscriptionId
At D:\TC_PS\Untitled1.ps1:5 char:1
+ Get-AzureVM
+ ~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzureVM], ArgumentNullException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand
You can start from here: Getting Started with Windows Azure PowerShell
Then you can simply run Get-AzureVM that will return all the virtual machines for every cloud service.
To stop a VM: Stop-AzureVM -ServiceName xxx -Name vm-test-01
To stop all the VMs in the subscription simply run: Get-AzureVM | Stop-AzureVM -Force
The -Force switch is necessary to stop the last virtual machine of the cloud service, otherwise the system would throw an error when trying to stop the last VM in the service, to avoid drop all the resources of your published app.
If you don't specify the -StayProvisioned switch, the virtual machine will be deallocated. This is the default behavior of Stop-AzureVM.

Azure deployment script keeps complaining "CurrentStorageAccount is not set"

I want to do automated deployment with Bamboo (a Continuous Integration solution). I found in here(http://www.windowsazure.com/en-us/develop/net/common-tasks/continuous-delivery/#script) a piece of script to help. The main code snippet:
#configure powershell with Azure 1.7 modules
Import-Module Azure
#configure powershell with publishsettings for your subscription
$pubsettings = $subscriptionDataFile
Import-AzurePublishSettingsFile $pubsettings
Set-AzureSubscription -CurrentStorageAccount $storageAccountName -SubscriptionName $selectedsubscription
write-host $storageAccountName
write-host "$selectedsubscription"
#set remaining environment variables for Azure cmdlets
$subscription = Get-AzureSubscription $selectedsubscription
$subscriptionname = $subscription.subscriptionname
$subscriptionid = $subscription.subscriptionid
$slot = $environment
#main driver - publish & write progress to activity log
Write-Output "$(Get-Date –f $timeStampFormat) - Azure Cloud Service deploy script started."
Write-Output "$(Get-Date –f $timeStampFormat) - Preparing deployment of $deploymentLabel for $subscriptionname with Subscription ID $subscriptionid."
Publish
$deployment = Get-AzureDeployment -slot $slot -serviceName $servicename
$deploymentUrl = $deployment.Url
Write-Output "$(Get-Date –f $timeStampFormat) - Created Cloud Service with URL $deploymentUrl."
Write-Output "$(Get-Date –f $timeStampFormat) - Azure Cloud Service deploy script finished."
I just first tried running this script from cmd by executing powershell E:\CI\OCZDeployment\publish_cloud_service.ps1 -environment Staging -serviceName "ocz" -storageAccountName "t2vsoft" -packageLocation OCZ.cspkg -cloudConfigLocation ServiceConfiguration.Cloud.cscfg -subscriptionDataFile E:\CI\OCZDeployment\SubscriptionDataFile.publishsettings -selectedsubscription "Development Subscription"
, where the publishsettings file is downloaded from Azure.
But I kept getting the error message:
New-AzureDeployment : CurrentStorageAccount is not set. Use Set-AzureSubscription subname -CurrentStorageAccount storageaccount to set it.
It has definitely been set in the snippet. I even tried copy & paste the Set-AzureSubscription line to inside New-AzureDeployment function. No luck.
Help me! Thank you!
Fixed. Don't know what was exactly the cause but i removed all registered azure subscriptions by executing:
PS> Get-AzureSubscription | ForEach-Object { Remove-AzureSubscription $_.SubscriptionName }
and re-imported the subscription by executing:
PS> Import-AzurePublishSettingsFile xxx.publishsettings
and it all worked! :)
There really were some similarly-named subscriptions in the subscription list. There might have been conflicts as such the azure scripts didn't know which one they really wanted.
If you have multiple subscriptions then you need to make sure the subscription that you want to use is set to the default. This can be done with the following:
Select-AzureSubscription -SubscriptionName "{subscriptionName}" -Default
if you aren't sure what your subscription name is you can use the following to see all of your subscriptions:
Get-AzureSubscription