How to Login without prompt?

44,524

Solution 1

You can use -Credential parameter, and DPAPI to login.

First, run the following PowerShell once to store a secured password for your account.

Read-Host "Enter Password" -AsSecureString | ConvertTo-SecureString `
-AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\Password.txt"

And then, you can use the following script to login.

# The azure account here must not be a Live ID.
$username = "<your Azure account>"
$SecurePassword = Get-Content "C:\Password.txt" | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential `
     -argumentlist $username, $SecurePassword

Login-AzureRmAccount -Credential $cred

An other way would be using Service Principal. First, you should follow the article to create a Service Principal

And then, use the following script to login.

$clientID = "<the client id of your AD Application>"
$key = "<the key of your AD Application>"
$SecurePassword = $key | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential `
     -argumentlist $clientID, $SecurePassword
$tenantID = "<the tenant id of your subscription>"

Add-AzureRmAccount -Credential $cred -TenantId $tenantID -ServicePrincipal

Solution 2

Might be late to post but I found another simple solution so listing it down here so as to help others:

  1. Login to azure account with command Login-AzureRmAccount.
  2. Save the context in Json file using command Save-AzureRmContext -Path "E:\AzureProfile.json".
  3. Now you can login without prompt using command: Import-AzureRmContext -Path "E:\AzureProfile.json".

Solution 3

If you are using a Live ID, then you can't log in without a prompt. You can't log in non-interactively.

Once you are logged in you can save your credentials with Save-AzureRmProfile this will save the login token to disk, that you can then use to login again with Select-AzureRmProfile However that token does expire, so you will need to log in again.

In order to login without being prompted at all you need to create an Azure Active Directory Account.

You can then use something like this

$cred = Get-Credential
Add-AzureRmAccount -Credential $cred

You can also build a credential object, so you can use this non-interactively.

Solution 4

You can use PowerShell ISE..

Follow this script:

$password = ConvertTo-SecureString 'Password' -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential ('Username', $password)

Connect-AzureRmAccount -Credential $Credential -Subscription 5a4dtert8-88bc-447c-bb20-b236terteb28e4 -Tenant 0d8trtec-5229-44ca-acc8-dbterte01b993b6

You can get auto generate the Connect-AzureRmAccount script using Power Shell ISE by

passing Subscription ID and Tenant with $credential variable..

enter image description here

Share:
44,524
H Bala
Author by

H Bala

Updated on April 03, 2020

Comments

  • H Bala
    H Bala about 4 years

    I tried following commands to get a noprompt faster login experience but each time i find login popup. I even tried using the certificate initially but since that didn't prove working, attempting with tenant id. Any help or suggestion on how to have seamless and faster login instead of interactive.

    Login-AzureRmAccount -SubscriptionId 1238154XXXXfd-1c4121796e58 -TenantId 754XXXXXXXXXXX5d10d8XXX
    
    Add-AzureRmAccount -Tenant "754XXXXXXXXXXX5d10d8XXX" -SubscriptionId "1238154XXXXfd-1c4121796e58"
    
    Login-AzureRmAccount -TenantId 754XXXXXXXXXXX5d10d8XXX
    

    Or, is it that I have to go via interactive login prompt always. Request pointers and thank in advance for consideration and time.

  • Michael B
    Michael B almost 8 years
    You don't have full control of permissions for an Azure Active Directory Account? (or a Live ID account for that matter) - and wouldn't it be at least a little better to use DPAPI to encrypt the credentials at rest? Rather than suggesting people hard code raw credentials into scripts
  • Jack Zeng
    Jack Zeng almost 8 years
    Thanks for Pointing it out. I will update my answer.
  • user2294401
    user2294401 over 6 years
    Things have changed: Save-AzureRmProfile -Force -Path ".\AzureProfile.json" and Select-AzureRmProfile -Path ".\AzureProfile.json" instead of Save-AzureRmContext and Import-AzureRmContext, respectively. [docs.microsoft.com/en-us/powershell/module/azurerm.profile/‌​…
  • Livven
    Livven over 6 years
    @rasx It's actually the other way around, your approach is the old one (from AzureRM PowerShell 3.8.0) and the one in the original answer is the new one.
  • Randy Minder
    Randy Minder over 6 years
    I am probably missing something but how is the Service Principal approach secure when you have to include your password, in the script, when creating the credential object?
  • zafar142003
    zafar142003 about 6 years
    Sadly Azure Active Directory Module for Powershell (AzureAD docs.microsoft.com/en-us/powershell/module/azuread/…) is only available for Windows 10 Windows 8.1 Pro Windows 8.1 Enterprise Windows 7 SP1 Windows Server 2016 TP5 Windows Server 2012 R2 Windows Server 2008 R2 SP1 as of now.
  • Harsimranjeet Singh
    Harsimranjeet Singh over 5 years
    though in few seconds its showing me error "Your Azure credentials have not been set up or have expired, please run Connect-AzureRmAccount to set up your Azure credentials."