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

Microsoft Office Online Server
Microsoft Office Online Server
Microsoft on-premises server product that runs Office Online. Previously known as Office Web Apps Server.
598 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,407 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,852 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Manu Philip 16,996 Reputation points MVP

    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 -Credential $psCred -Authentication Basic -AllowRedirection
    Import-PSSession $Session -AllowClobber -DisableNameChecking

    1 person found this answer helpful.

  2. Trevor Seward 11,691 Reputation points

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