question

DanielLeinads-7179 avatar image
0 Votes"
DanielLeinads-7179 asked JamesTran-MSFT edited

Connect-MsolService -Credential ignores $mycred = Get-Credential and prompts for credentials

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

azure-active-directorywindows-server-powershelloffice-online-server-generaloffice-online-server-exchange
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ManuPhilip avatar image
1 Vote"
ManuPhilip answered ManuPhilip commented

I would like to suggest the automation sample as below: Hope the following code will help you
$username = "admin@domain.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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your quick response.
Unfortunately, I receive an authentication error right after "Connect-MSolService -Credential $psCred":

Connect-MSolService : Authentication Error: Bad username or password.
In Zeile:1 Zeichen:1
+ Connect-MSolService -Credential $psCred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Connect-MsolService], Exception
+ FullyQualifiedErrorId : System.Exception,Microsoft.Online.Administration.Automation.ConnectMsolService

I double-checked the credentials, and they are correct. Any ideas?




0 Votes 0 ·

Microsoft's refence below is helpful to solve the issue. Looks like SSO is enabled for the account you are trying with
https://docs.microsoft.com/en-us/office365/troubleshoot/active-directory/connect-msoservice-throw-exception#cause

0 Votes 0 ·
trevorseward avatar image
0 Votes"
trevorseward answered

If you have MFA enabled, which you should, it is likely due to that. You need to interactively authenticate.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.