Skip to content

Commit

Permalink
Update dependencies, fix devicerow being required
Browse files Browse the repository at this point in the history
  • Loading branch information
llJochemll committed Nov 27, 2024
1 parent d860791 commit 0b5583d
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 111 deletions.
2 changes: 1 addition & 1 deletion GraphTools.Common/GraphHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public static GraphServiceClient GetGraphClient()
{
Name = "graphtools"
}
}));
}), ["DeviceManagementConfiguration.ReadWrite.All"]);
}
}
4 changes: 2 additions & 2 deletions GraphTools.Common/GraphTools.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Microsoft.Graph.Beta" Version="5.78.0-preview" />
<PackageReference Include="Azure.Identity" Version="1.13.1" />
<PackageReference Include="Microsoft.Graph.Beta" Version="5.86.0-preview" />
</ItemGroup>

</Project>
13 changes: 5 additions & 8 deletions GraphTools.GroupTags/DeviceRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ namespace GraphTools.GroupTags;

public class DeviceRow
{
[Name("Serial number", "Serienummer")]
public required string Serial { get; set; }

[Name("Group tag", "Groepstag")]
public string? Tag { get; set; }

[Name("Devicename")]
public string? Devicename { get; set; }
[Name("Serial number", "Serienummer")] public required string Serial { get; set; }

[Name("Group tag", "Groepstag")] public string? Tag { get; set; }

[Name("Devicename")] [Optional] public string? Devicename { get; set; }
}
4 changes: 2 additions & 2 deletions GraphTools.GroupTags/GraphTools.GroupTags.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Azure.Identity" Version="1.13.1" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="Microsoft.Graph.Beta" Version="5.78.0-preview" />
<PackageReference Include="Microsoft.Graph.Beta" Version="5.86.0-preview" />
</ItemGroup>

<ItemGroup>
Expand Down
95 changes: 57 additions & 38 deletions GraphTools.GroupTags/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,74 @@
using Microsoft.Graph;
using Microsoft.Graph.Beta.DeviceManagement.WindowsAutopilotDeviceIdentities.Item.UpdateDeviceProperties;
using Microsoft.Graph.Beta.Models;
using Microsoft.Graph.Beta.Models.ODataErrors;

Console.Write("CSV path:");
var path = Console.ReadLine();

if (path == null)
try
{
Console.WriteLine("Invalid path");
return;
}
Console.Write("CSV path:");
var path = Console.ReadLine();

if (path == null)
{
Console.WriteLine("Invalid path");
return;
}

var graphClient = GraphHelpers.GetGraphClient();
var graphClient = GraphHelpers.GetGraphClient();

var deviceIdentities = new List<WindowsAutopilotDeviceIdentity>();
var deviceIdentities = new List<WindowsAutopilotDeviceIdentity>();

var pageIterator = PageIterator<WindowsAutopilotDeviceIdentity, WindowsAutopilotDeviceIdentityCollectionResponse>
.CreatePageIterator(
graphClient,
(await graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities.GetAsync())!,
device =>
{
deviceIdentities.Add(device);
return true;
});
var pageIterator = PageIterator<WindowsAutopilotDeviceIdentity, WindowsAutopilotDeviceIdentityCollectionResponse>
.CreatePageIterator(
graphClient,
(await graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities.GetAsync())!,
device =>
{
deviceIdentities.Add(device);
return true;
});

await pageIterator.IterateAsync();
await pageIterator.IterateAsync();

using var reader = new StreamReader(path);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
using var reader = new StreamReader(path);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);

long requestCount = 0;
await foreach (var deviceRow in csv.GetRecordsAsync<DeviceRow>())
{
foreach (var matchingDeviceIdentity in deviceIdentities.Where(x => x.SerialNumber == deviceRow.Serial))
long requestCount = 0;
await foreach (var deviceRow in csv.GetRecordsAsync<DeviceRow>())
{
if (requestCount == 99)
foreach (var matchingDeviceIdentity in deviceIdentities.Where(x => x.SerialNumber == deviceRow.Serial))
{
await Task.Delay(TimeSpan.FromSeconds(20));
requestCount = 0;
if (requestCount == 99)
{
await Task.Delay(TimeSpan.FromSeconds(20));
requestCount = 0;
}

try
{
await graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities[matchingDeviceIdentity.Id]
.UpdateDeviceProperties.PostAsync(
new UpdateDevicePropertiesPostRequestBody
{
DisplayName = deviceRow.Devicename,
GroupTag = deviceRow.Tag
});
}
catch (ODataError e)
{
Console.WriteLine($"Row with serial: {deviceRow.Serial} failed");
}

requestCount++;
}

await graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities[matchingDeviceIdentity.Id]
.UpdateDeviceProperties.PostAsync(
new UpdateDevicePropertiesPostRequestBody
{
DisplayName = deviceRow.Devicename,
GroupTag = deviceRow.Tag
});
requestCount++;
}

Console.WriteLine("Done");
}
catch (Exception e)
{
Console.WriteLine(e);
}

Console.WriteLine("Done");
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
130 changes: 70 additions & 60 deletions GraphTools.RetireWipeFresh/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,83 +8,93 @@
using Microsoft.Graph.Beta.Models;
using Action = GraphTools.RetireWipeFresh.Action;

Console.Write("CSV path:");
var path = Console.ReadLine();

if (path == null)
try
{
Console.WriteLine("Invalid path");
return;
}
Console.Write("CSV path:");
var path = Console.ReadLine();

var graphClient = GraphHelpers.GetGraphClient();
if (path == null)
{
Console.WriteLine("Invalid path");
return;
}

var deviceIdentities = new List<WindowsAutopilotDeviceIdentity>();
var graphClient = GraphHelpers.GetGraphClient();

var pageIterator = PageIterator<WindowsAutopilotDeviceIdentity, WindowsAutopilotDeviceIdentityCollectionResponse>
.CreatePageIterator(
graphClient,
(await graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities.GetAsync())!,
device =>
{
deviceIdentities.Add(device);
return true;
});
var deviceIdentities = new List<WindowsAutopilotDeviceIdentity>();

await pageIterator.IterateAsync();
var pageIterator = PageIterator<WindowsAutopilotDeviceIdentity, WindowsAutopilotDeviceIdentityCollectionResponse>
.CreatePageIterator(
graphClient,
(await graphClient.DeviceManagement.WindowsAutopilotDeviceIdentities.GetAsync())!,
device =>
{
deviceIdentities.Add(device);
return true;
});

using var reader = new StreamReader(path);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
await pageIterator.IterateAsync();

long requestCount = 0;
await foreach (var deviceRow in csv.GetRecordsAsync<DeviceRow>())
{
if (deviceRow.Action != null)
using var reader = new StreamReader(path);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);

long requestCount = 0;
await foreach (var deviceRow in csv.GetRecordsAsync<DeviceRow>())
{
foreach (var matchingDeviceIdentity in deviceIdentities.Where(x => x.SerialNumber == deviceRow.Serial))
if (deviceRow.Action != null)
{
if (deviceRow.Action == null)
foreach (var matchingDeviceIdentity in deviceIdentities.Where(x => x.SerialNumber == deviceRow.Serial))
{
continue;
}
if (deviceRow.Action == null)
{
continue;
}

if (requestCount == 99)
{
await Task.Delay(TimeSpan.FromSeconds(20));
requestCount = 0;
}
if (requestCount == 99)
{
await Task.Delay(TimeSpan.FromSeconds(20));
requestCount = 0;
}

switch (deviceRow.Action)
{
case Action.Wipe:
await graphClient.DeviceManagement.ManagedDevices[matchingDeviceIdentity.ManagedDeviceId].Wipe
.PostAsync(new WipePostRequestBody
{
KeepEnrollmentData = deviceRow.KeepEnrollmentData,
KeepUserData = deviceRow.KeepUserData
});
break;
case Action.Retire:
await graphClient.DeviceManagement.ManagedDevices[matchingDeviceIdentity.ManagedDeviceId].Retire
.PostAsync();
break;
case Action.Fresh:
await graphClient.DeviceManagement.ManagedDevices[matchingDeviceIdentity.ManagedDeviceId]
.CleanWindowsDevice.PostAsync(
new CleanWindowsDevicePostRequestBody
switch (deviceRow.Action)
{
case Action.Wipe:
await graphClient.DeviceManagement.ManagedDevices[matchingDeviceIdentity.ManagedDeviceId].Wipe
.PostAsync(new WipePostRequestBody
{
KeepEnrollmentData = deviceRow.KeepEnrollmentData,
KeepUserData = deviceRow.KeepUserData
});
break;
case null:
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case Action.Retire:
await graphClient.DeviceManagement.ManagedDevices[matchingDeviceIdentity.ManagedDeviceId].Retire
.PostAsync();
break;
case Action.Fresh:
await graphClient.DeviceManagement.ManagedDevices[matchingDeviceIdentity.ManagedDeviceId]
.CleanWindowsDevice.PostAsync(
new CleanWindowsDevicePostRequestBody
{
KeepUserData = deviceRow.KeepUserData
});
break;
case null:
break;
default:
throw new ArgumentOutOfRangeException();
}

requestCount++;
requestCount++;
}
}
}

Console.WriteLine("Done");
}
catch (Exception e)
{
Console.WriteLine(e);
}

Console.WriteLine("Done");
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();

0 comments on commit 0b5583d

Please sign in to comment.