question

Vaikaumav-7085 avatar image
0 Votes"
Vaikaumav-7085 asked ChelseaWu-MSFT commented

Task scheduler credentials powershell

Hi I have written a ps script to set retention policy for SP sites. Wheras the script works, I need to make it non interactive. Any help will be highly appreciated...




Start-Transcript

install-module if does not exist

Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking
Import-Module ExchangeOnlineManagement

Variables for processing

$AdminCenterURL = "https://xyz-admin.sharepoint.com/"

User Name Password to connect

$AdminUserName = "@stgtenant.onmicrosoft.com"
$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

office-sharepoint-onlinewindows-server-powershell
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Vaikaumav-7085, do you have any progress on this thread? Please feel free to let us know if you need further assistance.

0 Votes 0 ·

Hi @Vaikaumav-7085, if you find an Answer helpful, please remember to accept it as answer.
It will help others who meet similar questions in this forum. Thank you for your understanding.

0 Votes 0 ·
trevorseward avatar image
0 Votes"
trevorseward answered

Create a tenant-scoped addin with Full Control permissions. Use the ClientId and ClientSecret switches for Connect-SPOService instead of a username and password.

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/authorization-and-authentication-of-sharepoint-add-ins

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ChelseaWu-MSFT avatar image
0 Votes"
ChelseaWu-MSFT answered ChelseaWu-MSFT commented

In addition to trevorseward’s Answer:

You can use Window Credential Manager to store and then pass the credentials when executing PowerShell commands.
Please refer to the link here for detailed information: Quick Authentication From PnP PowerShell To Access SharePoint.

Here is another post of discussion for your reference: How to avoid credential popup, to connect with graph API in the powershell script?


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This won't work for MFA and may not work depending on how the Task is set up in Task Scheduler.

0 Votes 0 ·

Thanks Trevor for the information.

0 Votes 0 ·