service account task scheduler

%3CLINGO-SUB%20id%3D%22lingo-sub-1662613%22%20slang%3D%22en-US%22%3Eservice%20account%20task%20scheduler%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1662613%22%20slang%3D%22en-US%22%3EHello%20awesome%20community%2C%20I%20have%20created%20a%20script%20to%20get%20private%20site%20collections%20and%20apply%20retention%20policies%20to%20them.%20This%20one%20want%20in%20scheduler.%20While%20the%20script%20works%20with%20mfa.%20Please%20help%20to%20get%20this%20working%20without%20mfa%20for%20service%20account.%3CBR%20%2F%3E_-------%3CBR%20%2F%3EStart-Transcript%3CBR%20%2F%3E%23install-module%20if%20does%20not%20exist%3CBR%20%2F%3EImport-Module%20Microsoft.Online.SharePoint.Powershell%20-DisableNameChecking%3CBR%20%2F%3EImport-Module%20ExchangeOnlineManagement%3CBR%20%2F%3E%3CBR%20%2F%3E%23Variables%20for%20processing%3CBR%20%2F%3E%24AdminCenterURL%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fstgtenant-admin.sharepoint.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstgtenant-admin.sharepoint.com%2F%3C%2FA%3E%22%3CBR%20%2F%3E%23User%20Name%20Password%20to%20connect%3CBR%20%2F%3E%24AdminUserName%20%3D%20%22%22%3CBR%20%2F%3E%24AdminPassword%20%3D%20%22%22%20%23App%20Password%3CBR%20%2F%3E%23create%20temp%20folder%20if%20it%20does%20not%20exist%3CBR%20%2F%3E%24ReportOutput%3D%22C%3A%5CTemp%5CpvtSiteColl.csv%22%3CBR%20%2F%3E%3CBR%20%2F%3EConnect-IPPSSession%20-UserPrincipalName%20%24AdminUserName%3CBR%20%2F%3E%3CBR%20%2F%3E%23Prepare%20the%20Credentials%3CBR%20%2F%3E%24SecurePassword%20%3D%20ConvertTo-SecureString%20%24AdminPassword%20-AsPlainText%20-Force%3CBR%20%2F%3E%24Cred%20%3D%20new-object%20-typename%20System.Management.Automation.PSCredential%20-argumentlist%20%24AdminUserName%2C%20%24SecurePassword%3CBR%20%2F%3E%23Connect%20to%20SharePoint%20Online%3CBR%20%2F%3EConnect-SPOService%20-url%20%24AdminCenterURL%3CBR%20%2F%3E%3CBR%20%2F%3E%23Get%20All%20site%20collections%3CBR%20%2F%3E%24SiteCollections%20%3D%20Get-SPOSite%20-Limit%20All%7C%20Where%20%7B%20%24_.Template%20-eq%20%22TEAMCHANNEL%230%22%7D%3CBR%20%2F%3EWrite-Host%20%22Total%20Number%20of%20Private%20Site%20collections%20Found%3A%22%24SiteCollections.count%20-f%20Yellow%3CBR%20%2F%3E%3CBR%20%2F%3E%23Array%20to%20store%20Result%3CBR%20%2F%3E%24ResultSet%20%3D%20%40()%3CBR%20%2F%3E%3CBR%20%2F%3E%23Loop%20through%20each%20site%20collection%20and%20retrieve%20details%3CBR%20%2F%3EForeach%20(%24Site%20in%20%24SiteCollections)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EWrite-Host%20%22Processing%20Site%20Collection%20%3A%22%24Site.URL%20-f%20Yellow%3CBR%20%2F%3E%3CBR%20%2F%3E%23Get%20site%20collection%20details%3CBR%20%2F%3E%24Result%20%3D%20new-object%20PSObject%3CBR%20%2F%3E%24Result%20%7C%20add-member%20-membertype%20NoteProperty%20-name%20%22Title%22%20-Value%20%24Site.Title%3CBR%20%2F%3E%24Result%20%7C%20add-member%20-membertype%20NoteProperty%20-name%20%22Url%22%20-Value%20%24Site.Url%3CBR%20%2F%3E%24Result%20%7C%20add-member%20-membertype%20NoteProperty%20-name%20%22Template%22%20-Value%20%24Site.Template%3CBR%20%2F%3E%3CBR%20%2F%3ESet-RetentionCompliancePolicy%20-Identity%20%22testFilesRetention%22%20-AddSharePointLocation%20%24Site.Url%3CBR%20%2F%3E%3CBR%20%2F%3E%24ResultSet%20%2B%3D%20%24Result%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%23Export%20Result%20to%20csv%20file%3CBR%20%2F%3E%24ResultSet%20%7C%20Export-Csv%20%24ReportOutput%20-notypeinformation%3CBR%20%2F%3E%3CBR%20%2F%3EWrite-Host%20%22private%20site%20Report%20Generated%20Successfully!%22%20-f%20Green%3CBR%20%2F%3E%3CBR%20%2F%3EStop-Transcript%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1662613%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMicrosoft%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor
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.
_-------
Start-Transcript
#install-module if does not exist
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking
Import-Module ExchangeOnlineManagement

#Variables for processing
$AdminCenterURL = "https://stgtenant-admin.sharepoint.com/"
#User Name Password to connect
$AdminUserName = ""
$AdminPassword = "" #App Password
#create temp folder if it does not exist
$ReportOutput="C:\Temp\pvtSiteColl.csv"

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

Stop-Transcript
0 Replies