16
votes

I have various scripts which I used to run in a wrapper to allow me to handle things like checking I had a valid connection and to ask the user if the connection/subscription currently selected is the one they want to run the script on before running it etc.

The classic commands have Clear-AzureProfile which allows me to run it from a script and effectively break the connection so that it can't be used again without calling Add-AzureAccount.

With the RM cmdlets I can only find Login-AzureRmAccount but once I've logged in...how do I call logout?

There doesn't appear to be a Logout-AzureRMAccount or Remove-AzureRMAccount and Clear-AzureProfile has no effect.

I run a dev workstation and connect to several different client subscriptions so want to be able to destroy the connection rather than just calling another login over the top of it (if that login failed I would still have the old connection set up which is dangerous for me)

I've just logged this at: https://msdn.microsoft.com/en-us/library/mt619248.aspx as from the documentation it looks like the commands may be lacking

8

8 Answers

8
votes

It appears the following works:

Set-AzureRmContext -Context ([Microsoft.Azure.Commands.Profile.Models.PSAzureContext]::new())
7
votes

UPDATE: For new powershell Azure "az" module, please use

Connect-AzAccount

for login and

Disconnect-AzAccount

for logout.

2
votes
> get-command -Module AzureRM.Profile

CommandType     Name                           
-----------     ----                           
Alias           Login-AzureRmAccount           
Alias           Select-AzureRmSubscription     
Cmdlet          Add-AzureRmAccount             
Cmdlet          Add-AzureRmEnvironment         
Cmdlet          Disable-AzureRmDataCollection  
Cmdlet          Enable-AzureRmDataCollection   
Cmdlet          Get-AzureRmContext             
Cmdlet          Get-AzureRmEnvironment         
Cmdlet          Get-AzureRmSubscription        
Cmdlet          Get-AzureRmTenant              
Cmdlet          Remove-AzureRmEnvironment      
Cmdlet          Save-AzureRmProfile            
Cmdlet          Select-AzureRmProfile          
Cmdlet          Set-AzureRmContext             
Cmdlet          Set-AzureRmEnvironment    

Note that Login-AzureRmAccount is an alias to Add-AzureRmAccount and there's no corresponding Remove.

Set-AzureRmContext might take a $null to clear the context, but I would be surprised if it doesn't instead just give an error.

2
votes

Directly closing your PS session would do. The AzureRM.Profile module won't persist your profile until you tell it to do so with Save-AzureRmProfile.

1
votes

In latest powershell version 1.0.1 MS hasn't provided any cmdlets which you are searching like Logout-AzureRMAccount or Remove-AzureRMAccount and Clear-AzureRMProfile.

In your case I would rather suggest a workaround for you.

Firstly close your PS window, delete your cahce and temp data. Secondly, Delete your name from Azure AD and then Add it back. Download a new publishsetting file and start doing afresh. Once you re-add the name it will treat everything as new for you.

1
votes

It's worth noting that you can always kick out an exception if the call to Login-AzureRmAccount fails to ensure your script doesn't continue with the old account:

Login-AzureRmAccount -ErrorAction Stop

The resulting dialog won't allow an invalid login. It will let you know if you're already logged in and give the choice to stay logged in with the current account or login with another, at which point the user still has to make some sort of conscious choice to proceed. If they choose to cancel out of the login dialog, the resulting exception will stop the script.

login-azurermaccount : authentication_canceled: User canceled authentication At line:1 char:1 + login-azurermaccount + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Add-AzureRmAccount], AadAuthenticationCanceledException + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.AddAzureRMAccountCommand

0
votes

I know this is an old question. But it looks like this has been updated with Remove-AzureAccount. You can read more about it here and Remove-AzureRMAccount found here.

0
votes

AzureRM context used to be bound to a powershell session, so just closing the console was enough.

But it seems latest versions of the powershell module have changed that.

Using the latest version of the powershell module, if you login in one console, you will also be logged in all the other consoles.

And you can log off using the new cmdlet Logout-AzureRmAccount.