Skip to content

Instantly share code, notes, and snippets.

@merlos
Created October 23, 2018 19:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merlos/e4dabae049dea4b92e070d11d0f7bfa7 to your computer and use it in GitHub Desktop.
Save merlos/e4dabae049dea4b92e070d11d0f7bfa7 to your computer and use it in GitHub Desktop.
A PowerShell Script to set credentials as environment variables. Password is not stored as plain text.
# Description
# This Script sets Sharepoint Credentials as environment variables so
# you are not prompted on your script every time
#
# Usage
#
# PS> SetSharepointCredentials
#
# It will prompt the user to type site url, username, password. If it is successful
#
# It will set the env variables SHAREPOINT_USER, SHAREPOINT_PASSWORD and SHAREPOINT_URL
#
# In other scripts it can be used in the following way:
# $URL = "http://dev.sharepoint.site.url/"
#
# # If environment variable with sharepoint URL is set, use it
# if (Test-Path env:SHAREPOINT_URL) {
# $URL = $env:SHAREPOINT_URL
# }
#
# # If sharepoint credentials are set on environment variables - use them
# if ((Test-Path env:SHAREPOINT_USER) -And (Test-Path env:SHAREPOINT_PASSWORD)) {
# # Convert from Encripted string to a SercurePassword Object
# $SecurePassword = ConvertTo-SecureString -String $env:SHAREPOINT_PASSWORD -Key (1..16)
# $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $env:SHAREPOINT_USER, $SecurePassword
# } else {
# # Ask for credentials
# $Credential = Get-Credential
# }
#
# Connect-PnPOnline -Url $URL -Credentials $Credential
# Write-Host "Connected to SharePoint site." -ForegroundColor Green
#
#
# --- INSTALl ----
# This should be run as Admin ( Start-Process powershell -Verb runAs )
if (-not (Get-Module -ListAvailable -Name SharePointPnPPowerShellOnline)) {
Install-Module SharePointPnPPowerShellOnline
}
# ----
Import-Module SharePointPnPPowerShellOnline
#
# Default Sharepoint site
#
$DefaultSharepointUrl = "https://xxx.sharepoint.com/sites/xxx"
#
# Default Sharepoint User
#
$DefaultSharepointUser = "xxx@xxx.onmicrosoft.com"
# By default we load whatever is on the env variable.
if (Test-Path env:SHAREPOINT_URL) {
$DefaultSharepointUrl = $env:SHAREPOINT_URL
}
if (Test-Path env:SHAREPOINT_USER) {
$DefaultSharepointUser = $env:SHAREPOINT_USER
}
# Prompt user to set its credentials
$Url = Read-Host "Sharepoint site URL (default: $DefaultSharepointUrl)"
# If user leaves the empty string => use the default
if ($Url -eq "") {
$Url = $DefaultSharepointUrl
}
$User = Read-Host "Username (default: $DefaultSharepointUser)"
if ($User -eq "") {
$User = $DefaultSharepointUser
}
$SecurePassword = Read-Host -assecurestring "Password for ($User)"
# Test the connection
Try {
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $SecurePassword
Connect-PnPOnline -Url $Url -Credentials $Credential
# If there is not problem with credentials save as env vars
$env:SHAREPOINT_URL = $Url
$env:SHAREPOINT_USER = $User
$EncriptedPassword = ConvertFrom-SecureString -SecureString $SecurePassword -Key (1..16)
$env:SHAREPOINT_PASSWORD = $EncriptedPassword
Write-Host "ENV variables of credentials Updated"
} Catch {
Write-Host "ERROR Logging in. Credentials were not updated"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment