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

Daniel Leinads 1 Reputation point
2021-01-19T15:05:04.33+00:00

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

Microsoft Office Online Server
Microsoft Office Online Server
Microsoft on-premises server product that runs Office Online. Previously known as Office Web Apps Server.
584 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,457 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Manu Philip 16,971 Reputation points MVP
    2021-01-19T17:16:52.737+00:00

    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

    1 person found this answer helpful.

  2. 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.