Skip to content

Commit 91bf0a7

Browse files
robinmeureVesaJuvonen
authored andcommitted
Added PowerShell snippet (SharePoint#3599)
1 parent cf1a678 commit 91bf0a7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

docs/solution-guidance/security-apponly-azuread.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,42 @@ namespace AzureADCertAuth
279279
}
280280
}
281281
```
282+
## Using this principal in your Powershell script using the PnP Sites core library
283+
When making use of Azure Automation Runbooks, first add the certificate (.pfx) using the Certificates option (under Shared Resources), then use the Get-AutomationCertificate cmdlet to retrieve the certificate to be used in the script.
284+
285+
> [!NOTE]
286+
> You need to add the SharePointPnPPowerShellOnline module to your Automation Account first. This module contains the OfficeDevPnP.Core.dll assembly needed to make the authentication call.
287+
288+
```powershell
289+
# path to installed modules
290+
$path = "C:\Modules\User\SharePointPnPPowerShellOnline"
291+
292+
# reference to needed assemblies
293+
Add-Type -Path "$path\Microsoft.SharePoint.Client.dll"
294+
Add-Type -Path "$path\Microsoft.SharePoint.Client.Runtime.dll"
295+
Add-Type -Path "$path\OfficeDevPnP.Core.dll"
296+
297+
# reference to the certificate
298+
$cert = Get-AutomationCertificate -Name 'NameOfCertificate'
299+
300+
# set the variables
301+
$siteUrl = "https://<tenant>.sharepoint.com"
302+
$appId = "<guid of the App>"
303+
$___domain = "<tenant>.onmicrosoft.com"
304+
$azureEnv = [OfficeDevPnP.Core.AzureEnvironment]::Production
305+
306+
# instantiate the objects
307+
$clientContext = new-object Microsoft.SharePoint.Client.ClientContext($siteUrl)
308+
$authManager = new-object OfficeDevPnP.Core.AuthenticationManager
309+
310+
# configure the object
311+
$clientContext = $authManager.GetAzureADAppOnlyAuthenticatedContext($siteUrl, $appId, $___domain, $cert, $azureEnv)
312+
313+
# do some stuff
314+
$clientContext.Load($clientContext.Web)
315+
$clientContext.ExecuteQuery()
316+
$clientContext.Web.Title
317+
```
282318

283319
## FAQ
284320
### Can I use other means besides certificates for realizing app-only access for my Azure AD app?

0 commit comments

Comments
 (0)