Office 365 compliance search multiple users script issue

Copper Contributor


I have been using the enclosed script to do bulk search of compliance in Exchange and create export of pst files for resigned users. But while scheduling this as task, the script stops after creating export for one user and wait for user confirmation with Yes or no prompt. Hence the task does not complete and wait in running stage itself. 

Can someone go through the script and let us know how to avoid this confirmation prompt? I tried looking at various posts but don't know where to put the $confirm:False value in the script.

Please help.


$CSVrecords = Get-MsolUser -All | Where-Object {$_.BlockCredential -eq $True -and $_.LastPasswordChangeTimestamp -lt (Get-Date) -and $_.LastPasswordChangeTimestamp -gt (Get-Date).AddDays(-2)} | select UserPrincipalName

$CSVrecords | out-file -filepath C:\Scripts\pstexport.txt -append

$userlist = get-content -path "C:\Scripts\PstExport.txt" -Delimiter "`t"

if ($CSVrecords -ne $null) {

#Get User Info
#$User = Read-host "Enter user name"

$Logfile = "C:\Scripts\User.txt"

Function LogWrite
Param ([string]$logstring)
Write-Host $LogString
Add-content $Logfile -value $logstring
$Date = Get-Date -Format "MM/dd/yyyy"
LogWrite $Date
LogWrite " "
#LogWrite "Username: $Csvrecords"

#Begin Compliance Search

Foreach ($CSVrecord in $CSVrecords){
$Username = $CSVrecord.UserPrincipalName
#$UPN = Get-AzureADUser -ObjectId $Username | ForEach-Object{$_.UserPrincipalName}
$SearchName = $Username -replace '',''
New-ComplianceSearch -Name $SearchName -ExchangeLocation $Username
Start-ComplianceSearch $SearchName
Logwrite " "
LogWrite "Compliance search $SearchName started"
$complianceSearch = Get-ComplianceSearch $SearchName | ForEach-Object{$_.Status}
Write-Host "Compliance Search in Progress"
Start-Sleep -s 30
While ($complianceSearch -ne 'Completed')
$Size = Get-ComplianceSearch $SearchName | ForEach-Object{$_.Size}
$Size = $Size / 1048576 | Out-String
$Size = $Size.SubString(0,6)

# Create Compliance Search in exportable format
New-ComplianceSearchAction -SearchName $SearchName -EnableDedupe $true -Export -Format FxStream -ExchangeArchiveFormat PerUserPst

#Wait for Export to complete
$ExportName = $SearchName + "_Export"
Start-Sleep -s 20
$SearchAction = Get-ComplianceSearchAction -Identity $ExportName | Select-Object Status,JobProgress
$Status = $SearchAction.Status
$ExportProgress = $SearchAction.JobProgress
Write-Host "Export in progress, $ExportProgress complete"
If($Status -ne "Completed"){
Start-Sleep -s 60
while ($Status -ne 'Completed')

LogWrite "Compliance search completed"

0 Replies