I would like to suggest the automation sample as below: Hope the following code will help you
$username = "admin@keyman .com"
$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
Import-Module MSOnline
Connect-MSolService -Credential $psCred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell-liveid/ -Credential $psCred -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber -DisableNameChecking
Connect-MsolService -Credential ignores $mycred = Get-Credential and prompts for credentials
Daniel Leinads
1
Reputation point
Hi, I use a variable to store my credentials: $mycred = Get-Credential and then try to pass the stored credentials to Connect-MsolService: Connect-MsolService -Credential $mycred But is still prompts me to enter the credentials, every time, no matter what I do. The credentials are 100% correct. Is this a bug, or am I missing something? I need it to work for my automation script. Best regards
2 answers
Sort by: Most helpful
-
Manu Philip 16,971 Reputation points MVP
2021-01-19T17:16:52.737+00:00 -
Trevor Seward 11,681 Reputation points
2021-01-20T18:02:17.957+00:00 If you have MFA enabled, which you should, it is likely due to that. You need to interactively authenticate.