Product: PowerShell Universal
Version: 3.9.3
The following Api works great as long as I only call it once and wait for it to complete before calling it again. The API registers a device to an Intune tenant using Graph API. If I do not wait and let it complete to register another device to 2 different tenants both the first and second systems are both registered to the tenant that was connected to on the first call. Any ideas how/why this would happen.
I have tried it using the integrated and PowerShell Environment. When the 2nd machine makes the call it will also cause an error on one or both devices. (write-information added for troubleshooting)
param([Parameter(Mandatory)]$AccountName, $SerialNumber, $DeviceHashData, $GroupTag )
Write-Information $DeviceHashData
write-information $AccountName
[xml]$xmlaccounts = Get-Content C:\ProgramData\UniversalAutomation\Repository\LCHFiles\AccountCreds.XML
$node = $xmlaccounts.Accounts.Account | Where-Object { $_.AccountName -eq “$AccountName”}
#Connect to graph using a cert
$ClientCertificate = Get-ChildItem -Path Cert:\LocalMachine\MY | Where-Object {$_.Subject -Match $LCHCertName}
$MsalToken = Get-MsalToken -AzureCloudInstance 1 -ClientId $node.AppId -TenantId $node.TenantID -ClientCertificate $ClientCertificate
Write-Information $node.appid
$connected = Connect-Graph -AccessToken $MsalToken.AccessToken
Construct Graph variables
$GraphVersion = "beta"
$GraphResource = "deviceManagement/importedWindowsAutopilotDeviceIdentities"
$GraphURI = "https://graph.microsoft.com/$($GraphVersion)/$($GraphResource)"
Construct hash table for new Autopilot device identity and convert to JSON
$AutopilotDeviceIdentity = [ordered]@{
'@odata.type' = '#microsoft.graph.importedWindowsAutopilotDeviceIdentity'
'groupTag' = if ($GroupTag) { "$($GroupTag)" } else { "" }
'serialNumber' = "$($SerialNumber)"
'productKey' = if ($ProductKey) { "$($ProductKey)" } else { "" }
'hardwareIdentifier' = "$($DeviceHashData)"
'assignedUserPrincipalName' = if ($UserPrincipalName) { "$($UserPrincipalName)" } else { "" }
'state' = @{
'@odata.type' = 'microsoft.graph.importedWindowsAutopilotDeviceIdentityState'
'deviceImportStatus' = 'pending'
'deviceRegistrationId' = ''
'deviceErrorCode' = 0
'deviceErrorName' = ''
}
}
$AutopilotDeviceIdentityJSON = $AutopilotDeviceIdentity | ConvertTo-Json
#upload the device
$response = Invoke-GraphRequest -uri $GraphURI -body $AutopilotDeviceIdentityJSON -Method Post -verbose -ContentType “application/json” -Headers @{Authorization = “Bearer $($MsalToken.AccessToken)” }
#Return the imported device ID to be used to get assignment query ID
$ret = Disconnect-Graph
Write-Information “Disconnected Graph”
$ret = Invoke-PSUScript -name WriteReglog.ps1 -RegStatus $response -AccountName $AccountName -TenantId $node.TenantID -DeviceHashId $DeviceHashData
$Response