New-AzCredProfile function from my Azure Automation Az-Module will set your PowerShell session to automatically login to your Azure account.
- The function will ask you to login to your Azure account.
- If the login was successful, the function will securely save your credentials to the JSON file, specified by
New-AzCredProfile –AzureProfilePath "$($env:USERPROFILE)\Documents\Azure.json"
- After that, the function injects four commands (lines) to your PowerShell profile script.
|1||Select-AzureRmProfile||Load Azure authentication info from a JSON secure file|
|2||Select-AzureRmSubscription||Interactively select Azure Subscription through
|3||Get-AzureRmResourceGroup||Interactively select Azure ResourceGroup through
|4||Get-AzureRmVM||Get VMs from your ResourceGroup with their
- The interactive selection functionality provided by Write-Menu function.
$AzResourceGroupvariable will be useful for other functions calls that require
–ResourceGroupNameparameter. Instead, you can utilize the
Select-AzResourceGrouphelper function, described in this article.
- To view what changes were made in your profile, use the
- You also can review these changes later.
notepad $PROFILE Invoke-Item $PROFILE
- Take a note! It is very important where you run the function. The reason is the fact that
$PROFILEvariable returns different values in the PowerShell console and PowerShell ISE.
- To see what is hidden behind the
$PROFILEvariable, open PowerShell console, type
$PROFILE.AllUsersAllHosts $PROFILE.AllUsersCurrentHost $PROFILE.CurrentUserAllHosts $PROFILE.CurrentUserCurrentHost
How it works
- The function returns
Trueif all done successfully.
To check the result open a new PowerShell console or reopen the PowerShell ISE.
For more details about the function, please take a look at the content based help and examples.
Get-Help New-AzCredProfile -Full Get-Help New-AzCredProfile -Examples Get-Help New-AzCredProfile -Parameter ShowProfile
- Do not forget to rerun the
New-AzCredProfilefunction every time you change your Azure password . In this case the function will return you
Falsebecause your profile already contains required commands, but secure JSON will be updated with new credentials.