Help....TAsk scheduler asking for credentials

Copper Contributor

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

4 Replies

Hello @vaibhavkaulkar 

Have you tried storing creds in an xml first, it is generally not a good idea to expose creds in the script.

If you want to try this, you can do below:-

-First store creds in an xml file
Get-Credential | Export-Clixml -Path "C:\Users\UserName\Desktop\Data\Cred.xml"
-Then Call them to connect your SP session
$Cred = (Import-Clixml "C:\Users\UserName\Desktop\Data\Cred.xml")
Connect-SPOService -Url $AdminURL -Credential $Cred

 

Hope this helps !!!

@DeepakRandhawa Thanks Deepak, I need to use this. So you mean- first time I set this up and then how do I read next time?

Hello @vaibhavkaulkar,

 

Here's a step by step process:-

 

- First Open Powershell and use below cmdlet to generate an XML file with credentials:-


          Get-Credential | Export-Clixml -Path "C:\Users\UserName\Desktop\Data\Cred.xml 

 

When you will use this you will be prompted to enter your credentials, just enter your credentials in the pop up box that appears, once you do you will be able to see an XML file at the location provided in the above cmdlet in the -path parameter.

If you open the XML you will see that it contains your username in plain text and your password in encrypted text.

Please remember to generate the XML file with same User profile on the same Computer where you will be using them as credentials are encrypted using the Windows Data Protection API and can only be used by user who generated them and on the computer on which they were generated.

 

- Second step is to start you SP script with below cmdlets :-

           

Start-Transcript

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

$Cred = (Import-Clixml "C:\Users\UserName\Desktop\Data\Cred.xml")
$AdminCenterURL = "https://xyz-admin.sharepoint.com/"
Connect-SPOService -Url $AdminURL -Credential $Cred
Connect-IPPSSession -Credential $Cred

#create temp folder if it does not exist
$ReportOutput="C:\Temp\pvtSiteColl.csv"


#Get All site collections
........

 

Thanks

Hello DeepakRandhawa,

 

I still get a pop up to enter credentials.