service account task scheduler

Copper Contributor
Hello awesome community, I have created a script to get private site collections and apply retention policies to them. This one want in scheduler. While the script works with mfa. Please help to get this working without mfa for service account.
#install-module if does not exist
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking
Import-Module ExchangeOnlineManagement

#Variables for processing
$AdminCenterURL = ""
#User Name Password to connect
$AdminUserName = ""
$AdminPassword = "" #App Password
#create temp folder if it does not exist

Connect-IPPSSession -UserPrincipalName $AdminUserName

#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL

#Get All site collections
$SiteCollections = Get-SPOSite -Limit All| Where { $_.Template -eq "TEAMCHANNEL#0"}
Write-Host "Total Number of Private Site collections Found:"$SiteCollections.count -f Yellow

#Array to store Result
$ResultSet = @()

#Loop through each site collection and retrieve details
Foreach ($Site in $SiteCollections)
Write-Host "Processing Site Collection :"$Site.URL -f Yellow

#Get site collection details
$Result = new-object PSObject
$Result | add-member -membertype NoteProperty -name "Title" -Value $Site.Title
$Result | add-member -membertype NoteProperty -name "Url" -Value $Site.Url
$Result | add-member -membertype NoteProperty -name "Template" -Value $Site.Template

Set-RetentionCompliancePolicy -Identity "testFilesRetention" -AddSharePointLocation $Site.Url

$ResultSet += $Result

#Export Result to csv file
$ResultSet | Export-Csv $ReportOutput -notypeinformation

Write-Host "private site Report Generated Successfully!" -f Green

0 Replies