Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $tenantId = "xxxxxxxxxxxxxxx"
- $clientId = "xxxxxxxxxxxxxxx"
- $clientSecret = "xxxxxxxxxxxxxxx"
- $UserPrincipalName = "xxxxxxxxxxxxxxx"
- $ManagedDeviceID = "xxxxxxxxxxxxxxx"
- Function Connect-ToGraph {
- <#
- .SYNOPSIS
- Authenticates to the Graph API via the Microsoft.Graph.Authentication module.
- .DESCRIPTION
- The Connect-ToGraph cmdlet is a wrapper cmdlet that helps authenticate to the Intune Graph API using the Microsoft.Graph.Authentication module. It leverages an Azure AD app ID and app secret for authentication or user-based auth.
- .PARAMETER Tenant
- Specifies the tenant (e.g. contoso.onmicrosoft.com) to which to authenticate.
- .PARAMETER AppId
- Specifies the Azure AD app ID (GUID) for the application that will be used to authenticate.
- .PARAMETER AppSecret
- Specifies the Azure AD app secret corresponding to the app ID that will be used to authenticate.
- .PARAMETER Scopes
- Specifies the user scopes for interactive authentication.
- .EXAMPLE
- Connect-ToGraph -TenantId $tenantID -AppId $app -AppSecret $secret
- -#>
- [cmdletbinding()]
- param
- (
- [Parameter(Mandatory = $false)] [string]$Tenant,
- [Parameter(Mandatory = $false)] [string]$AppId,
- [Parameter(Mandatory = $false)] [string]$AppSecret,
- [Parameter(Mandatory = $false)] [string]$scopes
- )
- Process {
- Import-Module Microsoft.Graph.Authentication
- $version = (get-module microsoft.graph.authentication | Select-Object -expandproperty Version).major
- if ($AppId -ne "") {
- $body = @{
- grant_type = "client_credentials";
- client_id = $AppId;
- client_secret = $AppSecret;
- scope = "https://graph.microsoft.com/.default";
- }
- $response = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$Tenant/oauth2/v2.0/token -Body $body
- $accessToken = $response.access_token
- $accessToken
- if ($version -eq 2) {
- write-host "Version 2 module detected"
- $accesstokenfinal = ConvertTo-SecureString -String $accessToken -AsPlainText -Force
- }
- else {
- write-host "Version 1 Module Detected"
- Select-MgProfile -Name Beta
- $accesstokenfinal = $accessToken
- }
- $graph = Connect-MgGraph -AccessToken $accesstokenfinal
- Write-Host "Connected to Intune tenant $TenantId using app-based authentication (Azure AD authentication not supported)"
- }
- else {
- if ($version -eq 2) {
- write-host "Version 2 module detected"
- }
- else {
- write-host "Version 1 Module Detected"
- Select-MgProfile -Name Beta
- }
- $graph = Connect-MgGraph -scopes $scopes
- Write-Host "Connected to Intune tenant $($graph.TenantId)"
- }
- }
- }
- function getallpagination () {
- <#
- .SYNOPSIS
- This function is used to grab all items from Graph API that are paginated
- .DESCRIPTION
- The function connects to the Graph API Interface and gets all items from the API that are paginated
- .EXAMPLE
- getallpagination -url "https://graph.microsoft.com/v1.0/groups"
- Returns all items
- .NOTES
- NAME: getallpagination
- #>
- [cmdletbinding()]
- param
- (
- $url
- )
- $response = (Invoke-MgGraphRequest -uri $url -Method Get -OutputType PSObject)
- $alloutput = $response.value
- $alloutputNextLink = $response."@odata.nextLink"
- while ($null -ne $alloutputNextLink) {
- $alloutputResponse = (Invoke-MGGraphRequest -Uri $alloutputNextLink -Method Get -outputType PSObject)
- $alloutputNextLink = $alloutputResponse."@odata.nextLink"
- $alloutput += $alloutputResponse.value
- }
- return $alloutput
- }
- Connect-ToGraph -Tenant $tenantId -AppId $clientId -AppSecret $clientSecret
- #Get managed device and check for primary user --> This works
- $URI = "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$ManagedDeviceID/users"
- $devicedetails = getallpagination -url $URI
- $PrimaryUser = $devicedetails.UserPrincipalName
- $PrimaryUserId = $devicedetails.id
- #Get AAD Id of primary user to assign -> This also works
- $URI= "https://graph.microsoft.com/beta/users/$UserPrincipalName"
- $userdetails = Invoke-MgGraphRequest -Uri $uri -Method GET -OutputType PSObject
- $UserID = $userdetails.id
- #Update Primary User on Managed Device -> This failes
- #Create required variables
- Write-Output "Updating primary user on Intune Device ID $ManagedDeviceID. New Primary User is $UserPrincipalName, ID: $UserID"
- $json = @"
- {
- "@odata.id": "https://graph.microsoft.com/beta/users/$UserID"
- }
- "@
- $URI = "https://graph.microsoft.com/beta/deviceManagement/managedDevices('$ManagedDeviceID')/users/`$ref"
- #Call Invoke-MsGraphCall
- $MSGraphCall = Invoke-MgGraphRequest -Uri $URI -Method POST -Body $json -OutputType PSObject -ContentType "application/json"
Advertisement
Add Comment
Please, Sign In to add comment