SOLVED

O365 Script - credentials prompt

%3CLINGO-SUB%20id%3D%22lingo-sub-61899%22%20slang%3D%22en-US%22%3EO365%20Script%20-%20credentials%20prompt%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-61899%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20long%20running%20scripts%20running%20against%20O365%20for%20our%20tenant.%20These%20can%20take%20a%20day%20or%20two%20to%20complete.%20%26nbsp%3BDuring%20the%20run%20of%20the%20script%2C%26nbsp%3Bthere%20can%20be%20an%20interuption%20where%20the%20credentials%20manager%20popup%20requests%20the%20credentials%20again%2C%20despite%20me%20having%20entered%20them%20at%20the%20beginning%20of%20the%20script.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can't%20find%20a%20way%20to%20handle%20this%20programatically%20so%20that%20the%20creds%20are%20returned%20and%20the%20script%20continues.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20anyone%20help%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere's%20the%20code%20for%20the%20connection%20at%20the%20start%20of%20the%20script%3A%3C%2FP%3E%3CPRE%3EImport-Module%20MSOnline%0A%24O365Cred%20%3D%20Get-Credential%0A%24O365Session%20%3D%20New-PSSession%20%E2%80%93ConfigurationName%20Microsoft.Exchange%20-ConnectionUri%20https%3A%2F%2Fps.outlook.com%2Fpowershell%20-Credential%20%24O365Cred%20-Authentication%20Basic%20-AllowRedirection%0AImport-PSSession%20%24O365Session%0AConnect-MsolService%20%E2%80%93Credential%20%24O365Cred%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-61899%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%20Administration%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66669%22%20slang%3D%22en-US%22%3ERe%3A%20O365%20Script%20-%20credentials%20prompt%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66669%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20Gorazd%20-%20that's%20about%20the%20same%20as%20what%20I%20use.%20My%20problem%20is%20that%20after%20a%20few%20hours%20the%20O365%20session%20goes%20into%20a%20broken%20state%20and%20the%20script%20stops.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20added%20some%20logic%20to%20remove%20the%20O365%20session%20and%20re-connect%20to%20O365%20every%2015%20mins%20to%20avoid%20this%20problem%2C%20and%20it%20looks%20promising.%20I%20have%20a%20script%20that%20has%20now%20been%20running%20for%20about%2018%20hours%20without%20stopping....%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-65188%22%20slang%3D%22en-US%22%3ERe%3A%20O365%20Script%20-%20credentials%20prompt%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-65188%22%20slang%3D%22en-US%22%3E%3CP%3E%3CEM%3EHello%2C%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3EI'm%20using%20this%20one%20and%20works%20great%20for%20me%20%3A%26nbsp%3B%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ESet-ExecutionPolicy%20RemoteSigned%0A%24UserCredential%20%3D%20Get-Credential%0A%24Session%20%3D%20New-PSSession%20-ConfigurationName%20Microsoft.Exchange%20-ConnectionUri%20https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%20-Credential%20%24UserCredential%20-Authentication%20Basic%20-AllowRedirection%0AImport-PSSession%20%24Session%3C%2FPRE%3E%3CP%3E%3CEM%3E%26nbsp%3B%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3E%26nbsp%3B%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-62879%22%20slang%3D%22en-US%22%3ERe%3A%20O365%20Script%20-%20credentials%20prompt%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-62879%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20the%20information%2C%20I'll%20look%20into%20using%20these%20techniques.%20%26nbsp%3BShame%26nbsp%3Bthere's%20no%20simple%20%22silver%20bullet%22%20here.....%20%3A(%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-62287%22%20slang%3D%22en-US%22%3ERe%3A%20O365%20Script%20-%20credentials%20prompt%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-62287%22%20slang%3D%22en-US%22%3E%3CP%3EThat's%20just%20a%20side%20effect%20of%20broken%20session%20to%20Exchange%20Online%20and%20PowerShell's%20attempt%20to%20recconect%20to%20it.%20The%20best%20thing%20you%20can%20do%20is%20to%20optimize%20your%20script%20to%20run%20as%20efficiently%20as%20possible%2C%20using%20server-side%20filtering%20and%20Invoke-Command%20in%20order%20to%20reduce%20the%20execution%20time.%20Apart%20from%20that%2C%20you%20can%20include%20a%20check%20for%20the%20session%20state%20before%20running%20each%20cmdlet%2Fiteration.%20An%20example%20of%20such%20script%20can%20be%20found%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2Fscriptcenter%2FStart-RobustCloudCommand-69fb349e%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgallery.technet.microsoft.com%2Fscriptcenter%2FStart-RobustCloudCommand-69fb349e%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20are%20some%20other%20recommenadations%20you%20can%20find%20here%3A%20%3CA%20href%3D%22https%3A%2F%2Fblogs.technet.microsoft.com%2Fexchange%2F2015%2F11%2F02%2Frunning-powershell-cmdlets-for-large-numbers-of-users-in-office-365%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fblogs.technet.microsoft.com%2Fexchange%2F2015%2F11%2F02%2Frunning-powershell-cmdlets-for-large-numbers-of-users-in-office-365%2F%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777124%22%20slang%3D%22en-US%22%3ERe%3A%20O365%20Script%20-%20credentials%20prompt%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777124%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F45372%22%20target%3D%22_blank%22%3E%40Joe%20McNicholas%3C%2FA%3E%26nbsp%3BCan%20I%20get%20the%20script%20that%20you%20modified%20and%20working%20perfectly%20fine%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Hi All

 

I have long running scripts running against O365 for our tenant. These can take a day or two to complete.  During the run of the script, there can be an interuption where the credentials manager popup requests the credentials again, despite me having entered them at the beginning of the script.

 

I can't find a way to handle this programatically so that the creds are returned and the script continues.

 

Can anyone help?

 

Here's the code for the connection at the start of the script:

Import-Module MSOnline
$O365Cred = Get-Credential
$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection
Import-PSSession $O365Session
Connect-MsolService –Credential $O365Cred
5 Replies
Highlighted
Best Response confirmed by Joe McNicholas (Occasional Contributor)
Solution

That's just a side effect of broken session to Exchange Online and PowerShell's attempt to recconect to it. The best thing you can do is to optimize your script to run as efficiently as possible, using server-side filtering and Invoke-Command in order to reduce the execution time. Apart from that, you can include a check for the session state before running each cmdlet/iteration. An example of such script can be found here: https://gallery.technet.microsoft.com/scriptcenter/Start-RobustCloudCommand-69fb349e

 

There are some other recommenadations you can find here: https://blogs.technet.microsoft.com/exchange/2015/11/02/running-powershell-cmdlets-for-large-numbers...

Highlighted

Thanks for the information, I'll look into using these techniques.  Shame there's no simple "silver bullet" here..... :(

Highlighted

Hello,

 

I'm using this one and works great for me : 

 

Set-ExecutionPolicy RemoteSigned
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

 

 

 

 

 

Highlighted

Thanks Gorazd - that's about the same as what I use. My problem is that after a few hours the O365 session goes into a broken state and the script stops.

 

I've added some logic to remove the O365 session and re-connect to O365 every 15 mins to avoid this problem, and it looks promising. I have a script that has now been running for about 18 hours without stopping....

Highlighted

@Joe McNicholas Can I get the script that you modified and working perfectly fine ?