SOLVED

Logging in to O365 using password file

Copper Contributor

I created a small piece of script I want to use to login to o365 remotely using powershell. 

 

The script will create the pssession and import the cmdlets but when I use a cmdlet the first time, it asks me for my password again.  I use this same code to create my credential to connect-msolservice, azure, etc. and it works fine.  Any ideas?  I added it to the attachments for better viewing also..

 

Here is the code:

$Myusername = (get-aduser -Identity $env:UserName).userprincipalname
$global:MyPassword = Get-Content "C:\test\test.txt" | ConvertTo-SecureString
$global:MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Myusername, $Mypassword
    
Write-Verbose -Message "Loading Session"
$SessionOffice365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $global:MyCredential -Authentication Basic -AllowRedirection
Write-Verbose -Message "Importing Session"
Import-PSSession $SessionOffice365 -AllowClobber -DisableNameChecking

 

2 Replies
best response confirmed by ibjames (Copper Contributor)
Solution

Nevermind..  when I start a new powershell instance, I did a get-pssession and notice that there was a ExchangeO365 module already being loaded, I removed it and now my code works..  :(  It must have been calling those cmdlets instead of the ones I was importing, even though I have clobber set

Isn’t it great figuring things out?? :)
1 best response

Accepted Solutions
best response confirmed by ibjames (Copper Contributor)
Solution

Nevermind..  when I start a new powershell instance, I did a get-pssession and notice that there was a ExchangeO365 module already being loaded, I removed it and now my code works..  :(  It must have been calling those cmdlets instead of the ones I was importing, even though I have clobber set

View solution in original post