Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import-module activedirectory
- Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
- $strOwnerName = $null
- $strGroupName = $null
- $newItem = $null
- $list = $null
- $list_2 = $null
- $allmembers = @()
- "Active Directory Module Initiated"
- $url = "http://myservername/Lists/SP_List_AD_Names/"
- $totalCount = 0
- #create the new Sharepoint Object we are working with
- $site = New-Object Microsoft.SharePoint.SPSite($url)
- #open it
- $web = $site.OpenWeb()
- #defines the list we are looking to to retrieve the names of AD Groups from
- $list = $web.Lists["SP_List_AD_Names"]
- # Reach into first list and grab the group names
- foreach ($listItem in $list.Items)
- {
- $strGroupName = $listItem.Name
- "Group is named: " + $strGroupName
- foreach ($groupname in $strGroupName)
- {
- $strGroupOwner = Get-ADGroup -identity $groupname -Properties ManagedBy | select managedby
- $strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select -ExpandProperty samaccountname
- "Group is owned by: " + $strOwnerName
- #reach out to AD, per group name, and return each user found in said group
- ForEach($Person in (Get-ADGroupMember $strGroupName -Recursive)){
- $totalcount ++
- $User = Get-ADUser $Person -Property description
- $AllMembers = New-Object PSObject -Property @{
- Name = $Person.Name
- Description = $User.Description
- NetworkID = $Person.SamAccountName
- Nested = $Null
- Group = $strGroupName
- Owner = $strOwnerName
- }
- #begin the second half of the process now that we have the users, they need to be fed into Sharepoint
- $url_2 = "http://myservername/"
- #create the second site object in Powershell
- $site_2 = New-Object Microsoft.SharePoint.SPSite($Url_2)
- #open it
- $web_2 = $site_2.OpenWeb()
- #define the second list which we will be adding information to
- $list_2 = $Web_2.Lists["SP_LIST_GROUP_MEMBERS"]
- #stick each person into our SP_LIST_GROUP_MEMBERS
- foreach($person in $AllMembers)
- {
- $newItem = $list_2.items.Add()
- $newItem["Title"] = $person."Name"
- "Persons name : " + $person."Name"
- $newItem["Description"] = $person."Description"
- $newItem["NetworkID"] = $person."NetworkID"
- $newItem["Nested"] = $person."Nested"
- $newItem["Group"] = $person."Group"
- $newItem["Owner"] = $person."Owner"
- # Commit the update, then loop again until end of file
- $newItem.Update()
- }
- }
- }
- }
- #display total iterations
- $totalcount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement