3

I am very new at Azure Powershell, I have been using Powershell for 2 weeks

Almost all of every morning, I have to "Add-AzureAccount" to log in, since after 12 hours, Azure Powershell automatically log out. This is what I understood

Add-AzureAccount : The given key was not present in the dictionary

However, this morning I do not need to Add-AzureAccount, It was already logged in, I have not used Azure Powershell for this weekends. This happens before, This really confused me, since I am having really hard time to understand this "12 hours automatically log out". One time, I had to uninstall/reinstall powershell

The reason Why I am asking this question is that my boss wants me to write a script that every one hour, Powershell gets info from Azure to CSV.file, so I made a script on Powershell, which runs every an hour by using Task Scheduler https://www.youtube.com/watch?v=oJ4nktysxnE, so I was concerned that my script won't work because this 12 hours log-out.

So I thought that I have to find a way to never log myself out from azure after 12 hours,because I thought every 12 hours, I had to do this "Add-AzureAccount processes"

but this morning I did not need to add-AzureAccount

My questions are

I made a script on Powershell runs every one hour like I mentioned above,

  1. Does powershell log me out automatically after 12 hours, in other words, after 12 hours after logged in, my script is not going to work?
  2. Is anywahy write a script never log me out, so I do not need worried about this 12 hours problem I am having and my script runs forever.

Thank you so much.

1
  • please share the PS code for better understanding of problem Dec 8, 2015 at 7:38

2 Answers 2

1

Can you please share the PS code which you are trying to run on hourly basis, this will be really helpful for understanding the issue.

  1. There is no hard limit after what time time account will be logged out automatically. logically, if that happens scheduling and automation might not have grown this big in Azure. It is very tedious to log in after every 12 hours and run the automation or scheduler again. Hope you understand.

  2. Instead keep a logic which allows getting the job triggered after every 12 hours using some infinite loop or or recursive calling. For Ex. Once the job is complete give it a sleep for 3600*12 seconds using PS code, then call it back.

    start-sleep -seconds 43200

0

You could try using the PublishSettings method of authentication, which will allow you to download a credentials file, load it into Powershell and then be authenticated until that credentials file is no longer valid. This is how I authenticate with Azure, and does not require additional maintenance.

Edit: if you have access to multiple Azure subscriptions, you may have to select the subscription you wish to work with by calling Select-AzureSubscription. This could be the first line of your powershell script if you required it.

2
  • 1
    I would not recommend going down this route as using publish settings file is currently discouraged (because of move to Azure Resource Manager which makes use of Azure AD from Azure Service Management) and I think it will shortly be deprecated. Dec 8, 2015 at 15:36
  • Thanks for your response Gaurav :) Dec 9, 2015 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.