Running a new compliance search seems to be having issues. The script does not produce errors while using the app registration we created in Azure but the Content Search in Purview doesn’t show any results.
When using user credentials we do not experience this issue so I suspect a permissions issue but I can’t seem to find where.
Any assistance would be greatly appreciated.
Full script below.
`# Get Primary Domain Controller (PDC) Server
$pdc = (Get-ADForest | Select-Object -ExpandProperty RootDomain | Get-ADDomain | Select-Object -Property PDCEmulator).PDCEmulator
# Set Destination Varibales for Output Files
$Root_Disk = "Z:"
$PSTArchive = "$($Root_Disk)PSTArchive"
$SharePath = ""
if (!(Test-Path $PSTArchive)) { New-Item $PSTArchive -ItemType Directory }
$Time = (Get-Date).ToString("yyyyMMddhhmm")
#################################################################################################
# Setup Output Logging
#################################################################################################
# Gets the current date and formats it to Year Month Day Hour Minute format as a time stamp
$time = (Get-Date).ToString("yyyyMMddhhmm")
[string]$script_name = $myInvocation.MyCommand
[string]$script_name
# Checks if path exist if not creates the directory
if (!(Test-Path "C:scriptslog")) { New-Item "C:scriptslog" -ItemType Directory }
#specify Log path
$log_directory = "C:scriptslog$(($script_name).Substring(0,(($script_name).length-4)))"
# Creates directory if it doesn't exists
if (!(Test-Path $log_directory)) { New-Item -Path $log_directory -ItemType Directory }
# This sets the default path to C:scriptslogsscript_name-datetime.log
Write-Host "Default Log file is $($log_directory)-$($time).log"
$log_path = "$($log_directory)-$($time).log"
# Logs all output to log path
Start-Transcript $log_path
Write-Host "
"
Write-Host ([Environment]::UserDomainName + "" + [Environment]::UserName) " Executed the script at $(Get-Date)`n`n"
#################################################################################################
#################################################################################################
# Set OU path to target employees
$Terminated_Users_OU = ""
# Get terminanted users from AD with extensionAttribute4 set to null
$TerminatedUsers = Get-ADUser -properties extensionAttribute4, enabled -Filter { enabled -eq $false -and -not(extensionAttribute4 -like "*") } -SearchBase $Terminated_Users_OU | Select-Object -First 5
# Connect to Exchange Online
Import-Module ExchangeOnlineManagement
$tenant_name = ""
$app_id = ""
$certificate_thumbprint = ""
Write-Host "Connecting to Exchange Online`n`n"
Connect-ExchangeOnline -CertificateThumbprint $certificate_thumbprint -AppId $app_id -Organization $tenant_name
Write-Host "Connect to Exhcnage Security `n`n"
Connect-IPPSSession -AppId $app_id -CertificateThumbprint $certificate_thumbprint -Organization $tenant_name
$connection_id = (Get-ConnectionInformation).ConnectionId
# Loop through each terminated user
foreach ($user in $TerminatedUsers)
{
# Use Try catch to check for emails
try
{
Get-EXOMailbox -Identity $user.UserPrincipalName -ErrorAction Stop
# Create PST file name with users UPN
$Formatted_User_Name = ($user.UserPrincipalName).Substring(0, ($user.UserPrincipalName).indexof("@") - 1)
# $PSTFileName = "$($user.UserPrincipalName.Replace("@","_"))_Archive.pst"
$PSTFileName = "$($Formatted_User_Name)_$($Time)_Archive.pst"
$PSTFilePath = Join-Path -Path $PSTArchive -ChildPath $PSTFileName
# Start Client Search
# Export mailbox to PST file
try
{
Write-Host "Exporting Mailbox to pst for $($user.UserPrincipalName) `n`n"
#New-MailboxExportRequst -Mailbox $user.UserPrincipalName -FilePath $PSTFilePath -ErrorAction Stop
New-ComplianceSearch "$($user.SamAccountName)_archive_box" -exchangelocation $user.UserPrincipalName | Start-ComplianceSearch
Get-ADUser $user.SamAccountName | Set-ADObject -Server $pdc -Replace @{extensionAttribute4 = "Exported" }
}
catch
{
# Do not flag the user as Exported since the job didn't complete
Write-Host "Exception happened `n`n"
}
}
catch
{
# Flag the user for NoMailBox since it can't be found in exchange online
Write-Host "User: $($user.UserPrincipalName) has no mailbox"
Get-ADUser $user.SamAccountName | Set-ADObject -Server $pdc -Replace @{extensionAttribute4 = "NoMailBox" }
}
}
# Move PST files to designated path
$PSTs = Get-ChildItem -Path $PSTArchive -File
Write-Host "Moving $($PSTs.count) PST Files`n"
foreach ($PST in $PSTs.FullName)
{
Write-Host "Moving $($PST.FullName) to $($SharePath)`n`n"
Move-Item -Path $PST -Destination $SharePath -Force
}
# Disconnect from Exchange Online
Write-Host "Disconnecting Session From Exchange Online`n"
Disconnect-ExchangeOnline -ConnectionId $connection_id -Confirm:$false
Stop-Transcript`
New contributor
cmeader1053 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.