Skip to content

Instantly share code, notes, and snippets.

@davehax
Last active June 26, 2023 11:33
Show Gist options
  • Save davehax/bf60c002bd58faf65f067cf8e27f422d to your computer and use it in GitHub Desktop.
Save davehax/bf60c002bd58faf65f067cf8e27f422d to your computer and use it in GitHub Desktop.
This is an example script that uses the Client Side Object Model (CSOM) in PowerShell to get the "Everyone except external users" Principal.
$siteUrl = "https://mysharepointsite.sharepoint.com/sites/subsite"
# Set headers
$headers = @{
"Authorization"="Bearer ";
}
$authRealm = "";
# Make an invalid request. We will extract the GUID from the Response.
try {
Invoke-WebRequest $("$siteUrl/_vti_bin/client.svc") -Method POST -Headers $headers
}
catch [exception] {
# Extract Authentication Realm GUID from Response headers
$_.Exception.Response.Headers["WWW-Authenticate"] -match '(?<=Bearer realm=")\w+-\w+-\w+-\w+-\w+(?=")'
$authRealm = $matches[0];
}
$everyoneExceptExternal = "c:0-.f|rolemanager|spo-grid-all-users/$authRealm"
$everyoneExceptExternalPrincipal = $web.EnsureUser($everyoneExceptExternal);
$ctx.Load($everyoneExceptExternalPrincipal);
$ctx.ExecuteQuery();
@jdumke-uwsp
Copy link

In line 20, what is the value of $web?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment