question

CleggMark-7768 avatar image
0 Votes"
CleggMark-7768 asked PhilipStone-4192 commented

Registering Devices for AutoPilot using Graph API

Hi

I'm trying to use the Graph API DeviceManagement.ImportedWindowsAutopilotDeivedIdentities in C# to register devices for Autopilot. I can create the GraphService object successfully, using an App registration, which has the correct permissions (DeviceManagementServiceConfig.ReadWrite.All) , but I can't get the new DeviceIdentity accepted.


 extern alias MSGraphBeta;
 using GraphBeta = MSGraphBeta.Microsoft.Graph;

 public async void EnrolDevice(string newSerialNumber, string newHash)
 {
     var GraphClient = Global.CreateBetaGraphServiceClient(); // Creates a GraphServiceClient object
    
     // Submit a registration request.
    
     var DeviceIdentity = new ImportedWindowsAutopilotDeviceIdentity()
     {
         SerialNumber = newSerialNumber,
         HardwareIdentifier = Encoding.ASCII.GetBytes(newHash),
         GroupTag = "",
         ProductKey = "",
         AssignedUserPrincipalName = "",
         State = new ImportedWindowsAutopilotDeviceIdentityState()
         {
             DeviceImportStatus = ImportedWindowsAutopilotDeviceIdentityImportStatus.Pending,
             DeviceRegistrationId = "",
             DeviceErrorCode = 0,
             DeviceErrorName = ""
         }
     };
    
     var ImportedDeviceIdentity = await GraphClient.DeviceManagement.ImportedWindowsAutopilotDeviceIdentities
         .Request()
         .AddAsync(DeviceIdentity); // <-- Exception thrown here.
 }

On hitting the .addAsync() call, I get "An error has occurred", with very little to say why.

From the exception message:

 Code: InternalError
 Message: {
   "_version": 3,
   "Message": "An error has occurred - Operation ID (for customer support): 00000000-0000-0000-0000-000000000000 - Activity ID: *xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx* - Url: https://fef.msua01.manage.microsoft.com/DeviceEnrollmentFE_2201/StatelessDeviceEnrollmentFEService/deviceManagement/importedWindowsAutopilotDeviceIdentities?api-version=5021-11-23",
   "CustomApiErrorPhrase": "",
   "RetryAfter": null,
   "ErrorSourceService": "",
   "HttpHeaders": "{}"
 }
 Inner error:
     AdditionalData:
     date: 2022-02-15T13:28:34
     request-id: *guid*
     client-request-id: *guid*
 ClientRequestId: *guid*

Has anybody had any experience doing this, and could offer any pointers?

The only thing that seems odd to me, is that the HardwareIdentifier has to be passed to the API as a byte[], where the value we get from WMI is a string, and the REST documentation also shows a string in the examples.

(I've tried both the v1.0 and Beta API entry points, with the same results).

Thanks.












mem-intune-graph
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@CleggMark-7768 Thanks for posting in our Q&A.

Based on my research, I find a Powershell packages about autopilot.
https://www.powershellgallery.com/packages/WindowsAutoPilotIntune/4.8/Content/WindowsAutoPilotIntune.psm1
Note: Non-Microsoft link, just for the reference.

For such Graph api issue, it is related to develop scope. With Q&A limitation resource, it is suggested to create a Premier support ticket to get more help. Here is the support link:
https://docs.microsoft.com/en-us/mem/get-support#premier-and-unified-support-customers

Thanks for understanding.

0 Votes 0 ·

Thanks for the response.
For now I've worked around the issue by calling the REST API directly using an HttpClient.

 HttpClient client = new HttpClient();
 client.BaseAddress = new Uri("https://graph.microsoft.com/beta/DeviceManagement/importedWindowsAutopilotDeviceIdentities");
 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "AccessToken");
    
 var Registration = new DeviceRegistration
 {
     serialNumber = "serial",
     hardwareIdentifier = "hashstring"
 };
 string JsonContent = System.Text.Json.JsonSerializer.Serialize(Registration);
 HttpResponseMessage response = await client.PostAsync("", new StringContent(JsonContent, Encoding.UTF8, "application/json"));
0 Votes 0 ·

@CleggMark-7768 Thanks for your kindness to share the solution. It is helpful for someone else who has the similar issue.

Thanks again and have a nice day. : )

0 Votes 0 ·

Hey,
just had the same issue and raised a question at GitHub. Your need to change DeviceIdentity to the code below

 HardwareIdentifier = Convert.FromBase64String(hash)





0 Votes 0 ·

0 Answers