Keep getting prompted to enter my credential (3 to 5 times)

#Get login credentials 
$UserCredential = Get-Credential 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid -Credential $UserCredential -Authentication Basic -AllowRedirection 
Import-PSSession $Session -AllowClobber -DisableNameChecking 
$Host.UI.RawUI.WindowTitle = $UserCredential.UserName + " (Office 365 Security & Compliance Center)"

#script begin
" " 
write-host "***********************************************"
write-host "   Office 365 Security & Compliance Center   " -foregroundColor yellow -backgroundcolor darkgreen
write-host "        eDiscovery cases - Holds report         " -foregroundColor yellow -backgroundcolor darkgreen 
write-host "***********************************************"
" " 

#prompt users to specify a path to store the output files
$time=get-date
$Path = Read-Host 'Enter a file path to save the report to a .csv file'
$outputpath=$Path+'\'+'CaseHoldsReport'+' '+$time.day+'-'+$time.month+'-'+$time.year+' '+$time.hour+'.'+$time.minute+'.csv'

#add case details to the csv file

function add-tocasereport{
Param([string]$casename,
[String]$casestatus,
[datetime]$casecreatedtime,
[string]$casemembers,
[datetime]$caseClosedDateTime,
[string]$caseclosedby,
[string]$holdname,
[String]$Holdenabled,
[string]$holdcreatedby,
[string]$holdlastmodifiedby,
[string]$ExchangeLocation,
[string]$sharePointlocation,
[string]$ContentMatchQuery,
[datetime]$holdcreatedtime,
[datetime]$holdchangedtime
)
$addRow = New-Object PSObject 
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Case name" -Value $casename
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Case status" -Value $casestatus
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Case members" -Value $casemembers
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Case created time" -Value $casecreatedtime
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Case closed time" -Value $caseClosedDateTime
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Case closed by" -Value $caseclosedby
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Hold name" -Value $holdname
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Hold enabled" -Value $Holdenabled
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Hold created by" -Value $holdcreatedby
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Hold last changed by" -Value $holdlastmodifiedby
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Exchange locations" -Value  $ExchangeLocation
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "SharePoint locations" -Value $sharePointlocation
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Hold query" -Value $ContentMatchQuery
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Hold created time (UTC)" -Value $holdcreatedtime
Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Hold changed time (UTC)" -Value $holdchangedtime

$allholdreport = $addRow | Select-Object "Case name","Case status","Hold name","Hold enabled","Case members", "Case created time","Case closed time","Case closed by","Exchange locations","SharePoint locations","Hold query","Hold created by","Hold created time (UTC)","Hold last changed by","Hold changed time (UTC)"

$allholdreport | export-csv -path $outputPath -notypeinfo -append -Encoding ascii 
}

#get information on the cases and pass values to the case report function

" "
write-host "Gathering a list of cases and holds..."
" "
$edc =Get-ComplianceCase -ErrorAction SilentlyContinue
foreach($cc in $edc)
{
write-host "Working on case :" $cc.name
if($cc.status -eq 'Closed')
{
$cmembers = ((Get-ComplianceCaseMember -Case $cc.name).windowsLiveID)-join ';'
add-tocasereport -casename $cc.name -casestatus $cc.Status -caseclosedby $cc.closedby -caseClosedDateTime $cc.ClosedDateTime -casemembers $cmembers 
}
else{
$cmembers = ((Get-ComplianceCaseMember -Case $cc.name).windowsLiveID)-join ';'
$policies = Get-CaseHoldPolicy -Case $cc.Name | %{ Get-CaseHoldPolicy $_.Name -Case $_.CaseId -DistributionDetail}
foreach ($policy in $policies)
{
$rule=Get-CaseHoldRule -Policy $policy.name
add-tocasereport -casename $cc.name -casemembers $cmembers -casestatus $cc.Status -casecreatedtime $cc.CreatedDateTime -holdname $policy.name -holdenabled $policy.enabled -holdcreatedby $policy.CreatedBy -holdlastmodifiedby $policy.LastModifiedBy -ExchangeLocation (($policy.exchangelocation.name)-join ';') -SharePointLocation (($policy.sharePointlocation.name)-join ';') -ContentMatchQuery $rule.ContentMatchQuery -holdcreatedtime $policy.WhenCreatedUTC -holdchangedtime $policy.WhenChangedUTC
}
}
}
" "
Write-host "Script complete! Report file: '$outputPath'"
" "
#script end

replace line 2 with:

Install-Module AZSBTools
$UserCredential = Get-SBCredential -UserName 'domain\user'

requires PowerShell version 5
replace ‘domain\user’ with desired user name

I ran above script every morning manually. I connect to Microsoft OK. I enter a period to save the file to local computer OK. The scripts start running OK then ask for my credential again and kicks few errors. I enter my credential again and it continues doing the same. After few times login finally finished and exports the .CSV file.

I need to make this script automatic as schedule task and email a group. I fixed the secure connecting to Microsoft. I got rid of entering a period by giving it a path. But I have not been able to fix the credential prompt entering my password. I opened a case with Microsoft and they have not been able to help.

the error I get is:
Starting a command on the remote server failed with the following error message : The I/O operation has been aborted because of either a thread exit or an application request. For
more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OperationStopped: (nam03b.ps.compl…ion.outlook.com:String) , PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure
+ PSComputerName : nam03b.ps.compliance.protection.outlook.com

Starting a command on the remote server failed with the following error message : The I/O operation has been aborted because of either a thread exit or an application request. For
more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OperationStopped: (nam03b.ps.compl…ion.outlook.com:String) , PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure
+ PSComputerName : nam03b.ps.compliance.protection.outlook.com

Thank you!

Sam, I used this link to setup secure login for this script:

https://o365info.com/connect-office-365-using-powershell-script-using-saved-encrypted-user-credentials/

Also what is inside Module AZSBTools ?

Above eDiscovery Case script is from this Microsoft link:

Use a script to create an eDiscovery holds report - Microsoft Purview (compliance) | Microsoft Docs .

I need to resolve below error which I have spent lots of time with no lock. Microsoft tech is saying they are not getting prompted to put their password in several times like I do. They say it may have to do with our network! Here is the error again:

Starting a command on the remote server failed with the following error message : The I/O operation has been aborted because of either a thread exit or an application request. For
more information, see the about_Remote_Troubleshooting Help topic.

  • CategoryInfo : OperationStopped: (nam03b.ps.compl…ion.outlook.com:String) , PSRemotingTransportException
  • FullyQualifiedErrorId : JobFailure
  • PSComputerName : nam03b.ps.compliance.protection.outlook.comStarting a command on the remote server failed with the following error message : The I/O operation has been aborted because of either a thread exit or an application request. For
    more information, see the about_Remote_Troubleshooting Help topic.
  • CategoryInfo : OperationStopped: (nam03b.ps.compl…ion.outlook.com:String) , PSRemotingTransportException
  • FullyQualifiedErrorId : JobFailure
  • PSComputerName : nam03b.ps.compliance.protection.outlook.com