Help....TAsk scheduler asking for credentials

%3CLINGO-SUB%20id%3D%22lingo-sub-1669473%22%20slang%3D%22en-US%22%3EHelp....TAsk%20scheduler%20asking%20for%20credentials%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1669473%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20I%20have%20written%20a%20ps%20script%20to%20set%20retention%20policy%20for%20SP%20sites.%20Wheras%20the%20script%20works%2C%20I%20need%20to%20make%20it%20non%20interactive.%20Any%20help%20will%20be%20highly%20appreciated...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E----------%3C%2FP%3E%3CP%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%3C%2FP%3E%3CP%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%20noopener%20noreferrer%20noopener%20noreferrer%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%40stgtenant.onmicrosoft.com%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%3C%2FP%3E%3CP%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%3C%2FP%3E%3CP%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%3C%2FP%3E%3CP%3EStop-Transcript%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1669473%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1671017%22%20slang%3D%22en-US%22%3ERe%3A%20Help....TAsk%20scheduler%20asking%20for%20credentials%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1671017%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F787013%22%20target%3D%22_blank%22%3E%40vaibhavkaulkar%3C%2FA%3E%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EHave%20you%20tried%20storing%20creds%20in%20an%20xml%20first%2C%20it%20is%20generally%20not%20a%20good%20idea%20to%20expose%20creds%20in%20the%20script.%3C%2FP%3E%3CP%3EIf%20you%20want%20to%20try%20this%2C%20you%20can%20do%20below%3A-%3C%2FP%3E%3CP%3E-First%20store%20creds%20in%20an%20xml%20file%3CBR%20%2F%3EGet-Credential%20%7C%20Export-Clixml%20-Path%20%22C%3A%5CUsers%5CUserName%5CDesktop%5CData%5CCred.xml%22%3CBR%20%2F%3E-Then%20Call%20them%20to%20connect%20your%20SP%20session%3CBR%20%2F%3E%24Cred%20%3D%20(Import-Clixml%20%22C%3A%5CUsers%5CUserName%5CDesktop%5CData%5CCred.xml%22)%3CBR%20%2F%3EConnect-SPOService%20-Url%20%24AdminURL%20-Credential%20%24Cred%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20helps%20!!!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New 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

3 Replies
Highlighted

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 !!!

Highlighted

@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?

Highlighted

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