The following forum(s) have migrated to Microsoft Q&A (Preview): Azure Active Directory!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
Can't get Azure credentials to stick in Powershell RRS feed

  • Question

  • Hi all,

    I am wanting to use Powershell (v4) to automate backup and restore of SQL Azure DB to Azure Storage. I am using certificate method....download publishsettings file for my subscription and import via PS console command Import-AzurePublishSettingsFile. I have verified that the cert is imported and registered on machine Get-Item cert:\\CurrentUser\My\$thumbprint.  Script is failing with "Get-AzureSqlDatabaseServer : Your Windows Azure credential in the Windows PowerShell session has expired. Please use Add-AzureAccount to login again." message.  If I authenticate in the console using Add-AzureAccount it all starts working again. Then if I close the session or let it time out and try to run it I get the prompt/error again. This obviously does not work for automating with scripts and I am in need of some help in solving this issue. Any help appreciated.

    Regards,

    Frank Silano
    Developer/Architect 
    BBI
    • Moved by Bill_Stewart Thursday, April 24, 2014 8:11 PM Move to more appropriate forum
    Thursday, April 24, 2014 8:03 PM

Answers

  • I'd try this:

    - Run REMOVE-AZUREACCOUNT first, before you start running your scripts, make sure you get rid of that azure account from your PS Session

    Then run your script using the publish settings file and cert mgmt. This should work. If you use Add-azureaccount it overwrites certificate authorization. If you remove-azureaccount it will fall back to certficiate authorization.

    Let me know how it goes - Matt Sampson


    R. Matt Sampson

    Thursday, April 24, 2014 8:51 PM

All replies

  • I'd try this:

    - Run REMOVE-AZUREACCOUNT first, before you start running your scripts, make sure you get rid of that azure account from your PS Session

    Then run your script using the publish settings file and cert mgmt. This should work. If you use Add-azureaccount it overwrites certificate authorization. If you remove-azureaccount it will fall back to certficiate authorization.

    Let me know how it goes - Matt Sampson


    R. Matt Sampson

    Thursday, April 24, 2014 8:51 PM
  • If anyone runs into this issue.. I found the solution on a blog

    http://itproguru.com/expert/2015/03/how-to-remove-azure-accounts-cached-credentials-from-powershell-remove-azureaccount-for-all-accounts-step-by-step/

    Execute:

    Get-AzureAccount | ForEach-Object { Remove-AzureAccount $_.ID -Force }

    Or in C#

                using (var runspace = RunspaceFactory.CreateRunspace())
                {
                    runspace.Open();
                    var scriptInvoker = new RunspaceInvoke(runspace);
    #if DEBUG
                    scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
    #endif
                    scriptInvoker.Invoke("Get-AzureAccount | ForEach-Object { Remove-AzureAccount $_.ID -Force }");

    ....

        }

    • Proposed as answer by bhavani_g Wednesday, March 15, 2017 10:13 PM
    Friday, November 27, 2015 9:49 AM
  • this worked for me.
    Wednesday, March 15, 2017 10:13 PM