Ejemplo n.º 1
0
        private async Task QueryAndDeleteEnrollmentGroups()
        {
            Console.WriteLine("\nCreating a query for enrollmentGroups...");
            QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollmentGroups");

            using (Query query = _provisioningServiceClient.CreateEnrollmentGroupQuery(querySpecification, QueryPageSize))
            {
                while (query.HasNext())
                {
                    Console.WriteLine("\nQuerying the next enrollmentGroups...");
                    QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);

                    var items = queryResult.Items;
                    foreach (EnrollmentGroup enrollment in items)
                    {
                        if (!groupEnrollmentsToBeRetained.Contains(enrollment.EnrollmentGroupId, StringComparer.OrdinalIgnoreCase))
                        {
                            Console.WriteLine($"EnrollmentGroup to be deleted: {enrollment.EnrollmentGroupId}");
                            _enrollmentGroupsDeleted++;
                            await _provisioningServiceClient.DeleteEnrollmentGroupAsync(enrollment.EnrollmentGroupId).ConfigureAwait(false);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static async Task RunSample()
        {
            Console.WriteLine("Starting sample...");

            using (ProvisioningServiceClient provisioningServiceClient =
                       ProvisioningServiceClient.CreateFromConnectionString(_provisioningConnectionString))
            {
                #region Create a new enrollmentGroup config
                Console.WriteLine("\nCreating a new enrollmentGroup...");
                string          certificatePassword = ReadCertificatePassword();
                var             certificate         = new X509Certificate2(_x509RootCertPath, certificatePassword);
                Attestation     attestation         = X509Attestation.CreateFromRootCertificates(certificate);
                EnrollmentGroup enrollmentGroup     =
                    new EnrollmentGroup(
                        _enrollmentGroupId,
                        attestation);
                Console.WriteLine(enrollmentGroup);
                #endregion

                #region Create the enrollmentGroup
                Console.WriteLine("\nAdding new enrollmentGroup...");
                EnrollmentGroup enrollmentGroupResult =
                    await provisioningServiceClient.CreateOrUpdateEnrollmentGroupAsync(enrollmentGroup).ConfigureAwait(false);

                Console.WriteLine("\nEnrollmentGroup created with success.");
                Console.WriteLine(enrollmentGroupResult);
                #endregion

                #region Get info of enrollmentGroup
                Console.WriteLine("\nGetting the enrollmentGroup information...");
                EnrollmentGroup getResult =
                    await provisioningServiceClient.GetEnrollmentGroupAsync(SampleEnrollmentGroupId).ConfigureAwait(false);

                Console.WriteLine(getResult);
                #endregion

                #region Query info of enrollmentGroup doc
                Console.WriteLine("\nCreating a query for enrollmentGroups...");
                QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollmentGroups");
                using (Query query = provisioningServiceClient.CreateEnrollmentGroupQuery(querySpecification))
                {
                    while (query.HasNext())
                    {
                        Console.WriteLine("\nQuerying the next enrollmentGroups...");
                        QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);

                        Console.WriteLine(queryResult);
                    }
                }
                #endregion

                #region Delete info of enrollmentGroup
                Console.WriteLine("\nDeleting the enrollmentGroup...");
                await provisioningServiceClient.DeleteEnrollmentGroupAsync(getResult).ConfigureAwait(false);

                #endregion
            }
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DpsName, DPSResources.RemoveEnrollmentGroup))
            {
                ProvisioningServiceDescription provisioningServiceDescription;
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName         = this.DpsObject.ResourceGroupName;
                    this.DpsName                   = this.DpsObject.Name;
                    provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject);
                }
                else
                {
                    if (ParameterSetName.Equals(ResourceIdParameterSet))
                    {
                        this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                        this.DpsName           = IotDpsUtils.GetIotDpsName(this.ResourceId);
                    }

                    provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName);
                }

                IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName);
                SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite);
                PSIotDpsConnectionString  psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName);
                ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString);

                try
                {
                    if (this.Name != null)
                    {
                        client.DeleteEnrollmentGroupAsync(this.Name).GetAwaiter().GetResult();
                    }
                    else
                    {
                        QueryResult enrollments = client.CreateEnrollmentGroupQuery(new QuerySpecification("select * from enrollments")).NextAsync().GetAwaiter().GetResult();
                        foreach (PSEnrollmentGroups enrollment in IotDpsUtils.ToPSEnrollmentGroups(enrollments.Items))
                        {
                            client.DeleteEnrollmentGroupAsync(enrollment.EnrollmentGroupId).GetAwaiter().GetResult();
                        }
                    }

                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(true);
                    }
                }
                catch
                {
                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(false);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public async Task EnableEnrollmentGroupAsync(string enrollmentGroupId)
        {
            QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollments"); // WHERE does not work here..., can only do a select ALL
            var groupEnrollments = await _provisioningServiceClient.CreateEnrollmentGroupQuery(querySpecification).NextAsync();

            foreach (var devicestring in groupEnrollments.Items)
            {
                var enrollment = devicestring as EnrollmentGroup;
                if (enrollment.EnrollmentGroupId == enrollmentGroupId)
                {
                    if (enrollment.ProvisioningStatus.Value != ProvisioningStatus.Enabled)
                    {
                        enrollment.ProvisioningStatus = ProvisioningStatus.Enabled;
                        var update = await _provisioningServiceClient.CreateOrUpdateEnrollmentGroupAsync(enrollment);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public async Task QueryEnrollmentGroupAsync()
        {
            _logger.LogInformation("Creating a query for enrollmentGroups...");
            QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollmentGroups");

            using (Query query = _provisioningServiceClient.CreateEnrollmentGroupQuery(querySpecification))
            {
                while (query.HasNext())
                {
                    _logger.LogInformation("Querying the next enrollmentGroups...");
                    QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);

                    _logger.LogInformation($"{queryResult}");

                    foreach (EnrollmentGroup group in queryResult.Items)
                    {
                        await EnumerateRegistrationsInGroup(querySpecification, group).ConfigureAwait(false);
                    }
                }
            }
        }