コード例 #1
0
        public override void ExecuteCmdlet()
        {
            IotHubDescription iotHubDescription;

            if (ParameterSetName.Equals(InputObjectParameterSet))
            {
                this.ResourceGroupName = this.InputObject.Resourcegroup;
                this.IotHubName        = this.InputObject.Name;
                iotHubDescription      = IotHubUtils.ConvertObject <PSIotHub, IotHubDescription>(this.InputObject);
            }
            else
            {
                if (ParameterSetName.Equals(ResourceIdParameterSet))
                {
                    this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId);
                    this.IotHubName        = IotHubUtils.GetIotHubName(this.ResourceId);
                }

                iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName);
            }

            IEnumerable <SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName);
            SharedAccessSignatureAuthorizationRule policy     = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryRead);
            PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName);
            RegistryManager          registryManager          = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString);

            if (this.DeviceId != null)
            {
                this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult()));
            }
            else
            {
                IList <Device>       devices       = new List <Device>();
                IEnumerable <string> deviceResults = registryManager.CreateQuery("Select * from Devices").GetNextAsJsonAsync().GetAwaiter().GetResult();
                foreach (string deviceResult in deviceResults)
                {
                    Device d = JsonConvert.DeserializeObject <Device>(deviceResult);
                    devices.Add(registryManager.GetDeviceAsync(d.Id).GetAwaiter().GetResult());
                }

                if (devices.Count == 1)
                {
                    this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(devices[0]));
                }
                else
                {
                    this.WriteObject(IotHubDataPlaneUtils.ToPSDevices(devices), true);
                }
            }
        }