Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save devangbhavsar89/204a15d147780a89b254162a7c561228 to your computer and use it in GitHub Desktop.
Save devangbhavsar89/204a15d147780a89b254162a7c561228 to your computer and use it in GitHub Desktop.
# Configuration Parameters (TODO: replace <token> placeholders with actual values before executing...)
$siteUrl = "https://<contoso>.sharepoint.com/sites/<contoso-electronics>"
$userName = "<adm-devang.bhavsar>@<contoso>.onmicrosoft.com"
$password = "<MySecretPassw0rd!>"
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $securePassword
# Establish connection with -ReturnConnection parameter
$connection = Connect-PnPOnline -Url $siteUrl -Credential $credentials -ReturnConnection
# Disconnect using -Connection parameter
Disconnect-PnPOnline -Connection $connection
# Attempt-1 to test context availablility
# Get-PnPWeb cmdlet fails on following step, which is an expected behaviour, as there is no active context available.
Write-Host "`nAttempt-1 to validate context availablility..."
Write-Host "`nGet-PnPWeb cmdlet fails on following step, which is an expected behaviour, as there is no active context available." -ForegroundColor Cyan
$titleInAttempt1 = (Get-PnPWeb).Title
Write-Host "`nTitle retrieved from 1st attempt: $titleInAttempt1"
Write-Host "`n----------------------------------------------------------------------------------------------------"
# Attempt-2 to test context availablility
# Get-PnPWeb cmdlet should also fail on subsequent step as well, however it doesn't fail but instead it returns web title, which is not an expected behaviour!
Write-Host "`nAttempt-2 to validate context availablility..."
Write-Host "`nGet-PnPWeb cmdlet should also fail on subsequent step as well, however it doesn't fail but instead it returns web title, which is not an expected behaviour!" -ForegroundColor Yellow
$titleInAttempt2 = (Get-PnPWeb).Title
Write-Host "`nTitle retrieved from 2nd attempt: `"$titleInAttempt2`" <--- Why this works?" -ForegroundColor Green
Write-Host "`n"
Disconnect-PnPOnline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment