Esempio n. 1
0
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                if (this.IsParameterBound(c => c.TargetGroupObject))
                {
                    TargetGroupObjectId = TargetGroupObject.Id;
                }
                else if (this.IsParameterBound(c => c.TargetGroupDisplayName))
                {
                    var targetGroup     = ActiveDirectoryClient.GetGroupByDisplayName(TargetGroupDisplayName);
                    TargetGroupObjectId = targetGroup.Id;
                }

                if (this.IsParameterBound(c => c.MemberUserPrincipalName))
                {
                    var memberObjectId = new List <Guid>();
                    foreach (var memberUPN in MemberUserPrincipalName)
                    {
                        memberObjectId.Add(ActiveDirectoryClient.GetObjectIdFromUPN(memberUPN));
                    }

                    MemberObjectId = memberObjectId.ToArray();
                }

                foreach (var memberObjectId in MemberObjectId)
                {
                    var groupAddMemberParams = new GroupAddMemberParameters()
                    {
                        Url = string.Format("{0}/{1}/directoryObjects/{2}",
                                            AzureEnvironmentConstants.AzureGraphEndpoint,
                                            AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant,
                                            memberObjectId)
                    };

                    if (ShouldProcess(target: memberObjectId.ToString(), action: string.Format("Adding user with object id '{0}' to group with object id '{1}'.", memberObjectId, TargetGroupObjectId)))
                    {
                        ActiveDirectoryClient.AddGroupMember(TargetGroupObjectId.ToString(), groupAddMemberParams);
                    }
                }

                if (PassThru.IsPresent)
                {
                    WriteObject(true);
                }
            });
        }