Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # PowerShell examples created by Marius Solbakken - http://goodworkaround.com/node/73
- #
- # Change to correct file location
- Add-Type -Path "C:\GraphAPI\Microsoft.IdentityModel.Clients.ActiveDirectory.2.14.201151115\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
- # Change these three values to your application and tenant settings
- $clientID = "26b2e067-291d-4ad7-9cd2-2e1fae15c905" # CLIENT ID for application
- $clientSecret = "qxUG3anGzOi9mfDoV7tHVNWOOM9k2FKo08Xs3bG4APs=" # KEY for application
- $tenant = "goodworkarounddemo.onmicrosoft.com" # The tenant domain name
- # Static values
- $resAzureGraphAPI = "https://graph.windows.net";
- $serviceRootURL = "https://graph.windows.net/$tenant"
- $authString = "https://login.windows.net/$tenant";
- # Creates a context for login.windows.net (Azure AD common authentication)
- [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$AuthContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$authString
- # Creates a credential from the client id and key
- [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]$clientCredential = New-Object -TypeName "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential"($clientID, $clientSecret)
- # Requests a bearer token
- $authenticationResult = $AuthContext.AcquireToken($resAzureGraphAPI, $clientCredential);
- # Output the token object
- Write-Host -ForegroundColor Yellow "Token object:"
- $authenticationResult | Format-List
- # Example to get all users
- Write-Host -ForegroundColor Yellow "Getting all users"
- $users = Invoke-RestMethod -Method GET -Uri "$serviceRootURL/users?api-version=1.5" -Headers @{Authorization=$authenticationResult.CreateAuthorizationHeader()} -ContentType "application/json"
- $users.value | Format-Table UserPrincipalName,DisplayName
- # Example to create a user
- Write-Host -ForegroundColor Yellow "Creating user"
- $newUserJSONObject = @{
- "accountEnabled" = $true
- "displayName" = "Donald Duck"
- "mailNickname" = "donald.duck"
- "passwordProfile" = @{
- "password" = "Test1234"
- "forceChangePasswordNextLogin" = $false
- }
- "userPrincipalName" = "donald.duck@$tenant"
- } | ConvertTo-Json
- Invoke-RestMethod -Method POST -Uri "$serviceRootURL/users?api-version=1.5" -Headers @{Authorization=$authenticationResult.CreateAuthorizationHeader()} -ContentType "application/json" -Body $newUserJSONObject
- # Example to update a user
- Write-Host -ForegroundColor Yellow "Updating user"
- $updateUserJSONObject = @{
- "givenName" = "Donald"
- "surname" = "Duck"
- } | ConvertTo-Json
- Invoke-RestMethod -Method PATCH -Uri "$serviceRootURL/users/donald.duck@${tenant}?api-version=1.5" -Headers @{Authorization=$authenticationResult.CreateAuthorizationHeader()} -ContentType "application/json" -Body $updateUserJSONObject
- # Example to get a single user
- Write-Host -ForegroundColor Yellow "Getting user"
- $user = Invoke-RestMethod -Method GET -Uri "$serviceRootURL/users/donald.duck@${tenant}?api-version=1.5" -Headers @{Authorization=$authenticationResult.CreateAuthorizationHeader()} -ContentType "application/json"
- $user
- # Example to delete a user - please note that this requires a special permissions set with the MsOnline PowerShell module
- Write-Host -ForegroundColor Yellow "Deleting user"
- Invoke-RestMethod -Method DELETE -Uri "$serviceRootURL/users/donald.duck@${tenant}?api-version=1.5" -Headers @{Authorization=$authenticationResult.CreateAuthorizationHeader()} -ContentType "application/json"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement