|
# 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" |
|
} |