public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            try
            {
                var userPrincipalName = string.Format("{0}|{1}", ClaimIdentifier, this.UserName);


                LogVerbose("Querying UserProfile service");
                GetUserProfileInformation(userPrincipalName);



                LogVerbose("Querying UserProfiles.PeopleManager");
                GetUserInformation(userPrincipalName);


                using (var runspace = new SPIaCRunspaceWithDelegate(SPIaCConnection.CurrentConnection))
                {
                    LogVerbose("Executing runspace to query Get-MSOLUser(-UserPrincipalName {0})", this.UserName);

                    var getUserCommand = new PCommand.Command("Get-MsolUser");
                    getUserCommand.Parameters.Add((new PCommand.CommandParameter("UserPrincipalName", this.UserName)));

                    var results = runspace.ExecuteRunspace(getUserCommand, string.Format("Unable to get user with UserPrincipalName : " + userPrincipalName));
                    if (results.Count() > 0)
                    {
                        foreach (PSObject itemUser in results)
                        {
                            var userProperties = itemUser.Properties;

                            GetPSObjectValue(userProperties, "DisplayName");
                            GetPSObjectValue(userProperties, "FirstName");
                            GetPSObjectValue(userProperties, "LastName");
                            GetPSObjectValue(userProperties, "UserPrincipalName");
                            GetPSObjectValue(userProperties, "Department");
                            GetPSObjectValue(userProperties, "Country");
                            GetPSObjectValue(userProperties, "UsageLocation");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogError(ex, "Failed to execute QueryUserProfile in tenant {0}", this.ClientContext.Url);
            }
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            try
            {
                using (var runspace = new SPIaCRunspaceWithDelegate(SPIaCConnection.CurrentConnection))
                {
                    if (string.IsNullOrEmpty(this.GroupId))
                    {
                        var getGroupAllCommand = new PCommand.Command("Get-MSOLGroup");
                        getGroupAllCommand.Parameters.Add((new PCommand.CommandParameter("All")));

                        LogVerbose("BEGIN ---------------");
                        LogVerbose("Executing runspace to query Get-MSOLGroup(-All) which will take a minute or two.");
                        var groupAllResults = runspace.ExecuteRunspace(getGroupAllCommand, string.Format("Unable to retrieve {0} groups", "All"));
                        if (groupAllResults.Count() > 0)
                        {
                            LogVerbose("MSOL Groups found {0}", groupAllResults.Count());
                        }
                        LogVerbose("END ---------------");
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(this.SiteUrl))
                        {
                            using (var thisContext = this.ClientContext.Clone(this.SiteUrl))
                            {
                                thisContext.Credentials = this.ClientContext.Credentials;

                                var qroups = thisContext.Web.SiteGroups;
                                thisContext.Load(qroups);

                                var groupName = qroups.GetByName(this.GroupName);
                                thisContext.Load(groupName, gg => gg.LoginName, gg => gg.Title, gg => gg.Users);
                                thisContext.ExecuteQuery();
                                LogVerbose("Group {0} found in Site {1}", groupName.Title, this.SiteUrl);
                                foreach (var user in groupName.Users)
                                {
                                    LogVerbose("LoginName {0}", user.LoginName);
                                    LogVerbose("Email {0}", user.Email);
                                    LogVerbose("Id {0}", user.Id);
                                    LogVerbose("PrincipalType {0}", user.PrincipalType);
                                    LogVerbose("Title {0}", user.Title);
                                    LogVerbose("UserId {0}", user.UserId);
                                }
                            }
                        }

                        var getGroupCommand = new PCommand.Command("Get-MSOLGroup");
                        getGroupCommand.Parameters.Add((new PCommand.CommandParameter("ObjectId", this.GroupId)));
                        getGroupCommand.Parameters.Add((new PCommand.CommandParameter("Verbose")));

                        LogVerbose("BEGIN ---------------");
                        LogVerbose("Executing runspace to query Get-MSOLGroup(-SearchString {0}).", this.GroupId);
                        var groupresults = runspace.ExecuteRunspace(getGroupCommand, string.Format("Unable to retrieve {0} groups", this.GroupId));
                        if (groupresults.Count() > 0)
                        {
                            foreach (var itemGroup in groupresults)
                            {
                                var groupProperties = itemGroup.Properties;
                                var groupObjectId   = GetPSObjectValue(groupProperties, "ObjectId");

                                GetPSObjectValue(groupProperties, "CommonName");
                                GetPSObjectValue(groupProperties, "DisplayName");
                                GetPSObjectValue(groupProperties, "EmailAddress");
                                GetPSObjectValue(groupProperties, "GroupType");
                                GetPSObjectValue(groupProperties, "LastDirSyncTime");
                                GetPSObjectValue(groupProperties, "ManagedBy");
                                GetPSObjectValue(groupProperties, "ValidationStatus");
                                GetPSObjectValue(groupProperties, "IsSystem");

                                if (GroupMembership)
                                {
                                    var getGroupMembershipCommand = new PCommand.Command("Get-MsolGroupMember");
                                    getGroupMembershipCommand.Parameters.Add((new PCommand.CommandParameter("GroupObjectId", groupObjectId)));
                                    getGroupMembershipCommand.Parameters.Add((new PCommand.CommandParameter("Verbose")));

                                    LogVerbose("BEGIN ---------------");
                                    LogVerbose("Executing runspace to query Get-MsolGroupMember(-GroupObjectId {0}).", groupObjectId);
                                    var groupMembershipResults = runspace.ExecuteRunspace(getGroupMembershipCommand, string.Format("Unable to retrieve {0} group membership", this.GroupId));
                                    if (groupMembershipResults.Count() > 0)
                                    {
                                        foreach (var itemMember in groupMembershipResults)
                                        {
                                            var memberProperties = itemMember.Properties;

                                            GetPSObjectValue(memberProperties, "CommonName");
                                            GetPSObjectValue(memberProperties, "DisplayName");
                                            GetPSObjectValue(memberProperties, "EmailAddress");
                                            GetPSObjectValue(memberProperties, "GroupMemberType");
                                            GetPSObjectValue(memberProperties, "ObjectId");
                                        }
                                    }
                                    LogVerbose("END ---------------");
                                }
                            }
                        }
                        LogVerbose("END ---------------");
                    }
                }
            }
            catch (Exception ex)
            {
                LogError(ex, "Failed to execute QueryUserProfile in tenant {0}", this.ClientContext.Url);
            }
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            var models = new List <SPGroupDefinitionModel>();

            try
            {
                using (var runspace = new SPIaCRunspaceWithDelegate())
                {
                    runspace.Initialize(SPIaCConnection.CurrentConnection, "MSOnline", "Connect-MsolService");

                    var getGroupCommand = new PCommand.Command("Get-MSOLGroup");

                    if (string.IsNullOrEmpty(this.GroupId))
                    {
                        getGroupCommand.Parameters.Add((new PCommand.CommandParameter("All")));
                    }
                    else
                    {
                        getGroupCommand.Parameters.Add((new PCommand.CommandParameter("ObjectId", this.GroupId)));
                        getGroupCommand.Parameters.Add((new PCommand.CommandParameter("Verbose")));
                    }


                    LogVerbose("BEGIN ---------------");
                    LogVerbose("Executing runspace to query Get-MSOLGroup(-All) which will take a minute or two.");
                    var collectionOfGroups = runspace.ExecuteRunspace(getGroupCommand, string.Format("Unable to retrieve {0} groups", "All"));
                    LogVerbose("END ---------------");

                    if (collectionOfGroups.Count() > 0)
                    {
                        LogVerbose("MSOL Groups found {0}", collectionOfGroups.Count());

                        foreach (var itemGroup in collectionOfGroups)
                        {
                            var groupProperties = itemGroup.Properties;
                            var groupObjectId   = groupProperties.GetPSObjectValue("ObjectId");

                            var model = new SPGroupMSOnlineDefinition()
                            {
                                ObjectId         = groupObjectId,
                                Title            = groupProperties.GetPSObjectValue("CommonName"),
                                Description      = groupProperties.GetPSObjectValue("DisplayName"),
                                EmailAddress     = groupProperties.GetPSObjectValue("EmailAddress"),
                                GroupType        = groupProperties.GetPSObjectValue("GroupType"),
                                LastDirSyncTime  = groupProperties.GetPSObjectValue("LastDirSyncTime"),
                                ManagedBy        = groupProperties.GetPSObjectValue("ManagedBy"),
                                ValidationStatus = groupProperties.GetPSObjectValue("ValidationStatus"),
                                IsSystem         = groupProperties.GetPSObjectValue("IsSystem")
                            };


                            if (GroupMembership)
                            {
                                var getGroupMembershipCommand = new PCommand.Command("Get-MsolGroupMember");
                                getGroupMembershipCommand.Parameters.Add((new PCommand.CommandParameter("GroupObjectId", groupObjectId)));
                                getGroupMembershipCommand.Parameters.Add((new PCommand.CommandParameter("Verbose")));

                                LogVerbose("BEGIN ---------------");
                                LogVerbose("Executing runspace to query Get-MsolGroupMember(-GroupObjectId {0}).", groupObjectId);
                                var groupMembershipResults = runspace.ExecuteRunspace(getGroupMembershipCommand, string.Format("Unable to retrieve {0} group membership", this.GroupId));
                                if (groupMembershipResults.Count() > 0)
                                {
                                    foreach (var itemMember in groupMembershipResults)
                                    {
                                        var memberProperties = itemMember.Properties;
                                        var userModel        = new SPUserDefinitionModel()
                                        {
                                            UserName         = memberProperties.GetPSObjectValue("CommonName"),
                                            UserDisplay      = memberProperties.GetPSObjectValue("DisplayName"),
                                            UserEmail        = memberProperties.GetPSObjectValue("EmailAddress"),
                                            Organization     = memberProperties.GetPSObjectValue("GroupMemberType"),
                                            MSOnlineObjectId = memberProperties.GetPSObjectValue("ObjectId")
                                        };
                                        model.Users.Add(userModel);
                                    }
                                }
                            }

                            models.Add(model);
                        }
                    }
                }

                models.ForEach(groups => WriteObject(groups));
            }
            catch (Exception ex)
            {
                LogError(ex, "Failed to execute QueryUserProfile in tenant {0}", this.ClientContext.Url);
            }
        }