Example #1
0
        private async Task CheckGroupDevicesAreUpToDateStepAsync(string groupId, string provider, string name, string version, bool isCompliant)
        {
            ConsoleEx.WriteLine(ConsoleColor.Yellow, $"Check group {groupId} device compliance with update {provider}/{name}/{version}...");
            bool updateFound = false;
            int  counter     = 0;

            do
            {
                var groupResponse = _managementClient.GetBestUpdatesForGroupsAsync(groupId);
                await foreach (var updatableDevices in groupResponse)
                {
                    var updatableDevicesDoc = JsonDocument.Parse(updatableDevices.ToMemory());
                    if (updatableDevicesDoc.RootElement.TryGetProperty("updateId", out var updateId))
                    {
                        if (updateId.GetProperty("provider").GetString() == provider &&
                            updateId.GetProperty("name").GetString() == name &&
                            updateId.GetProperty("version").GetString() == version)
                        {
                            updateFound = true;
                            var deviceCount = updatableDevicesDoc.RootElement.GetProperty("deviceCount").GetInt32();
                            if (isCompliant)
                            {
                                if (deviceCount == 0)
                                {
                                    Console.WriteLine("All devices within the group have this update installed.");
                                }
                                else
                                {
                                    Console.WriteLine($"There are still {deviceCount} devices that can be updated to update {provider}/{name}/{version}.");
                                }
                            }
                            else
                            {
                                Console.WriteLine($"There are {deviceCount} devices that can be updated to update {provider}/{name}/{version}.");
                            }
                        }
                    }
                }

                counter++;
                if (!updateFound)
                {
                    Console.Write(".");
                    await Task.Delay(DefaultRetryAfterValue);
                }
            } while (!updateFound && counter <= 6);

            if (!updateFound)
            {
                Console.WriteLine("Update is still not available for any group device.");
            }
            Console.WriteLine();
        }