示例#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);

            IList <Device> parentDevices = new List <Device>();

            if (this.DeviceId != null)
            {
                Device parentDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult();

                if (parentDevice == null)
                {
                    throw new ArgumentException($"The entered parent device \"{this.DeviceId}\" doesn't exist.");
                }

                if (!parentDevice.Capabilities.IotEdge)
                {
                    throw new ArgumentException($"The entered device \"{this.DeviceId}\" should be an edge device.");
                }

                parentDevices.Add(parentDevice);
            }
            else
            {
                IEnumerable <string> deviceResults = registryManager.CreateQuery(IotHubDataPlaneUtils.GetEdgeDevices()).GetNextAsJsonAsync().GetAwaiter().GetResult();
                foreach (string deviceResult in deviceResults)
                {
                    Device d = JsonConvert.DeserializeObject <Device>(deviceResult);
                    parentDevices.Add(registryManager.GetDeviceAsync(d.Id).GetAwaiter().GetResult());
                }
            }

            IList <PSDevicesChildren> psDevicesChildren = new List <PSDevicesChildren>();

            foreach (Device parentDevice in parentDevices)
            {
                PSDevicesChildren psDeviceChildren = new PSDevicesChildren
                {
                    DeviceId         = parentDevice.Id,
                    ChildrenDeviceId = new List <string>()
                };

                IList <Device>       childDevices  = new List <Device>();
                IEnumerable <string> deviceResults = registryManager.CreateQuery(IotHubDataPlaneUtils.GetNonEdgeDevices(parentDevice.Scope)).GetNextAsJsonAsync().GetAwaiter().GetResult();

                foreach (string deviceResult in deviceResults)
                {
                    Device d = JsonConvert.DeserializeObject <Device>(deviceResult);
                    psDeviceChildren.ChildrenDeviceId.Add(d.Id);
                }

                psDevicesChildren.Add(psDeviceChildren);
            }

            this.WriteObject(psDevicesChildren, true);
        }