Esempio n. 1
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            var models = new List <SPGroupDefinitionModel>();

            try
            {
                var qroups = this.ClientContext.Web.SiteGroups;
                this.ClientContext.Load(qroups);

                var groupName = qroups.GetByName(this.GroupName);
                this.ClientContext.Load(groupName, gg => gg.LoginName, gg => gg.Title, gg => gg.Users);
                this.ClientContext.ExecuteQuery();

                var model = new SPGroupDefinitionModel()
                {
                    Title       = groupName.LoginName,
                    Description = groupName.Title
                };

                LogVerbose("Group {0} found in Site {1}", groupName.Title, this.ClientContext.Web.Url);

                if (GroupMembership)
                {
                    foreach (var user in groupName.Users)
                    {
                        var userModel = new SPUserDefinitionModel()
                        {
                            UserName      = user.LoginName,
                            UserEmail     = user.Email,
                            GuidId        = user.Id,
                            PrincipalType = user.PrincipalType,
                            UserDisplay   = user.Title,
                            UserId        = user.UserId
                        };
                        model.Users.Add(userModel);
                        LogVerbose("LoginName {0}", user.LoginName);
                    }
                }

                models.Add(model);

                models.ForEach(group => WriteObject(group));
            }
            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 <SPUserDefinitionModel>();

            try
            {
                TenantContext.EnsureProperties(tssp => tssp.RootSiteUrl);
                var TenantUrl       = TenantContext.RootSiteUrl.EnsureTrailingSlashLowered();
                var MySiteTenantUrl = TenantUrl.Replace(".sharepoint.com", "-my.sharepoint.com");

                var i       = 1;
                var ilogger = new DefaultUsageLogger(LogVerbose, LogWarning, LogError);
                var odfb    = this.ClientContext.GetOneDriveSiteCollections(ilogger, MySiteTenantUrl, true);

                models = odfb.Select(s =>
                {
                    var UserName = s.UserName;
                    if (!string.IsNullOrEmpty(UserName))
                    {
                        UserName = UserName.ToString().Replace(";", ",");
                    }

                    var userObject = new SPUserDefinitionModel()
                    {
                        UserName  = UserName,
                        OD4BUrl   = s.Url,
                        UserIndex = i++
                    };

                    return(userObject);
                }).ToList();
            }
            catch (Exception ex)
            {
                LogError(ex, "Failed to retreive user profiles");
            }

            WriteObject(models, true);
        }
Esempio n. 3
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            var models = new List <SPUserDefinitionModel>();

            try
            {
                LogVerbose("Querying UserProfiles.PeopleManager");
                var userPrincipalName = string.Format("i:0#.f|membership|{0}", this.UserName);
                var peopleContext     = new Microsoft.SharePoint.Client.UserProfiles.PeopleManager(this.ClientContext);
                var personProperties  = peopleContext.GetPropertiesFor(userPrincipalName);
                this.ClientContext.Load(personProperties);
                this.ClientContext.ExecuteQuery();

                if (personProperties != null)
                {
                    var profileProperties = personProperties.UserProfileProperties.ToList();
                    LogVerbose("Display Name: {0}", personProperties.DisplayName);
                    var model = new SPUserDefinitionModel()
                    {
                        UserDisplay          = personProperties.DisplayName,
                        UserEmail            = personProperties.Email,
                        LatestPost           = personProperties.LatestPost,
                        OD4BUrl              = personProperties.PersonalUrl,
                        UserProfileGUID      = profileProperties.GetPropertyValue("UserProfile_GUID"),
                        SPSDistinguishedName = profileProperties.GetPropertyValue("SPS-DistinguishedName"),
                        SPSSid           = profileProperties.GetPropertyValue("SID"),
                        MSOnlineObjectId = profileProperties.GetPropertyValue("msOnline-ObjectId")
                    };
                    models.Add(model);
                }

                models.ForEach(user => WriteObject(user));
            }
            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);
            }
        }
Esempio n. 5
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();
            var models = new List <SPUserDefinitionModel>();

            try
            {
                var creds      = SPIaCConnection.CurrentConnection.GetActiveCredentials();
                var newcreds   = new System.Net.NetworkCredential(creds.UserName, creds.Password);
                var spourl     = new Uri(this.ClientContext.Url);
                var spocreds   = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(creds.UserName, creds.Password);
                var spocookies = spocreds.GetAuthenticationCookie(spourl);

                var spocontainer = new System.Net.CookieContainer();
                spocontainer.SetCookies(spourl, spocookies);

                var ows = new OfficeDevPnP.Core.UPAWebService.UserProfileService();
                ows.Url             = string.Format("{0}/_vti_bin/userprofileservice.asmx", spourl.AbsoluteUri);
                ows.Credentials     = newcreds;
                ows.CookieContainer = spocontainer;
                var UserProfileResult = ows.GetUserProfileByIndex(-1);
                var NumProfiles       = ows.GetUserProfileCount();
                var i              = 1;
                var tmpCount       = 0;
                var nextValue      = UserProfileResult.NextValue;
                var nextValueIndex = int.Parse(nextValue);

                // As long as the next User profile is NOT the one we started with (at -1)...
                while (nextValueIndex != -1)
                {
                    LogVerbose("Examining profile {0} of {1}", i, NumProfiles);

                    // Look for the Personal Space object in the User Profile and retrieve it
                    // (PersonalSpace is the name of the path to a user's OneDrive for Business site. Users who have not yet created a
                    // OneDrive for Business site might not have this property set.)

                    tmpCount++;

                    var PersonalSpaceUrl = UserProfileResult.RetrieveUserProperty("PersonalSpace");
                    var UserName         = UserProfileResult.RetrieveUserProperty("UserName");
                    if (!string.IsNullOrEmpty(UserName))
                    {
                        UserName = UserName.ToString().Replace(";", ",");
                    }

                    var userObject = new SPUserDefinitionModel()
                    {
                        UserName  = UserName,
                        OD4BUrl   = PersonalSpaceUrl,
                        UserIndex = nextValueIndex
                    };
                    models.Add(userObject);

                    // And now we check the next profile the same way...
                    UserProfileResult = ows.GetUserProfileByIndex(nextValueIndex);
                    nextValue         = UserProfileResult.NextValue;
                    nextValueIndex    = int.Parse(nextValue);
                    i++;
                }

                models.ForEach(user => WriteObject(user));
            }
            catch (Exception ex)
            {
                LogError(ex, "Failed to retreive user profiles");
            }
        }