Advertisement
Guest User

Untitled

a guest
May 2nd, 2014
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import-module activedirectory
  2. Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
  3. $strOwnerName = $null
  4. $strGroupName = $null
  5. $newItem = $null
  6. $list = $null
  7. $list_2 = $null
  8. $allmembers = @()
  9. "Active Directory Module Initiated"
  10.  
  11.  
  12. $url = "http://myservername/Lists/SP_List_AD_Names/"
  13.  
  14. $totalCount = 0
  15.  
  16. #create the new Sharepoint Object we are working with
  17. $site = New-Object Microsoft.SharePoint.SPSite($url)
  18.  
  19. #open it
  20. $web = $site.OpenWeb()
  21.  
  22. #defines the list we are looking to to retrieve the names of AD Groups from
  23. $list = $web.Lists["SP_List_AD_Names"]
  24.  
  25. # Reach into first list and grab the group names
  26. foreach ($listItem in $list.Items)
  27. {        
  28. $strGroupName = $listItem.Name
  29. "Group is named: " + $strGroupName
  30. foreach ($groupname in $strGroupName)
  31.     {    
  32.     $strGroupOwner = Get-ADGroup -identity $groupname -Properties ManagedBy | select managedby
  33.     $strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select -ExpandProperty samaccountname
  34.     "Group is owned by: " + $strOwnerName
  35.    
  36.     #reach out to AD, per group name, and return each user found in said group
  37.    
  38.         ForEach($Person in (Get-ADGroupMember $strGroupName -Recursive)){
  39.         $totalcount ++
  40.         $User = Get-ADUser $Person -Property description
  41.         $AllMembers = New-Object PSObject -Property @{
  42.    
  43.             Name = $Person.Name
  44.             Description = $User.Description
  45.             NetworkID = $Person.SamAccountName
  46.             Nested = $Null
  47.             Group = $strGroupName
  48.             Owner = $strOwnerName            
  49.                                                        }
  50.    
  51.     #begin the second half of the process now that we have the users, they need to be fed into Sharepoint
  52.     $url_2 = "http://myservername/"
  53.     #create the second site object in Powershell
  54.     $site_2 = New-Object Microsoft.SharePoint.SPSite($Url_2)
  55.     #open it
  56.     $web_2 = $site_2.OpenWeb()
  57.     #define the second list which we will be adding information to
  58.     $list_2 = $Web_2.Lists["SP_LIST_GROUP_MEMBERS"]
  59.    
  60.     #stick each person into our SP_LIST_GROUP_MEMBERS
  61.     foreach($person in $AllMembers)
  62.     {
  63.    
  64.        
  65.             $newItem = $list_2.items.Add()
  66.            
  67.      
  68.             $newItem["Title"] = $person."Name"
  69.                
  70.             "Persons name : " + $person."Name"
  71.      
  72.             $newItem["Description"] = $person."Description"
  73.      
  74.  
  75.      
  76.             $newItem["NetworkID"] = $person."NetworkID"  
  77.      
  78.      
  79.             $newItem["Nested"] = $person."Nested"
  80.      
  81.      
  82.             $newItem["Group"] = $person."Group"
  83.      
  84.      
  85.             $newItem["Owner"] = $person."Owner"
  86.      
  87.  
  88.         # Commit the update, then loop again until end of file
  89.        
  90.         $newItem.Update()
  91.        
  92.        
  93.     }
  94.                                                        }    
  95.     }    
  96. }
  97.  
  98.     #display total iterations
  99.     $totalcount
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement