3

I'm using the following SharePoint Online CSOM powershell that connects to my Office 365 tenant (E3). I'm using the Read-host to enter the password when I run the script and the script works fine...

If I plan to execute the script unattended (task scheduler) on a daily/weekly how do I authenticate without manual entering the password or how can I use the default network credentials to authenticate O365?

I don't want to hard-code password in the script file. I hope this is a very common scenario...

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$siteUrl = "https://contoso.sharepoint.com"
$adminUsername = "admin@contoso.onmicrosoft.com"
$secureAdminPassword = Read-Host -AsSecureString "please enter password"
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adminUsername, $secureAdminPassword)
7

We've done it similar to this for remote scripting by service accounts:

  • Log into your server as the service account
  • Execute a script similar to this:

$cred = get-credential
$cred | Export-clixml c:\some\path

  • Update your powershell profile or include in your script with:
$credential = Import-Clixml  "C:\some\path\securePassword.xml"
  • Set up any scheduled scripts and utilize $credential.Username and $credential.Password like:

    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)

The file is encrypted using a cert of the user who created the credential. So if using for it automation, make sure to create cred using the account that will be accessing the file.

2

This is covered by the Office Dev Patterns and Practices group. Read the section "Setting Up Credentials" in this repo.

https://github.com/OfficeDev/PnP-PowerShell

0

Sorry I'm late, but still high in search results...

1) Add credentials as generic Windows Credential in Credential Manager. [https://github.com/SharePoint/PnP-PowerShell/wiki/How-to-use-the-Windows-Credential-Manager-to-ease-authentication-with-PnP-PowerShell]

2) Install SharePointPnPPowerShellOnline [https://github.com/SharePoint/PnP-PowerShell/releases]

3) The PS code:

$cred = Get-PnPStoredCredential -Name <WindowCredLabel> -Type PSCredential $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)

-2

You can convert to secure string

$plainPassword = "Plain text"
$secureString = ConvertTo-SecureString $plainPassword -AsPlainText -Force
  • 3
    No, just no. You don't want to hard code admin credentials into scripts that can be read on the server – Eric Alexander Sep 8 '16 at 14:47
  • 3
    Don't store passwords in plain text, yes it works but it's a HUGE security hole – GavinB Sep 8 '16 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.