Advertisement
Guest User

Untitled

a guest
Jul 16th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #Add - PSSnapin Microsoft.SharePoint.PowerShell
  2.  
  3. $host.Runspace.ThreadOptions = "ReuseThread"
  4.  
  5. Function Get-SiteGroup-Owners
  6. {
  7. param ($sCSOMPath,$sSiteUrl,$sUserName,$sPassword,$FileName)
  8. try
  9. {
  10. $sCSOMRuntimePath=$sCSOMPath + "Microsoft.SharePoint.Client.Runtime.dll"
  11. $sCSOMPathdll=$sCSOMPath + "Microsoft.SharePoint.Client.dll"
  12. Add-Type -Path $sCSOMPathdll
  13. Add-Type -Path $sCSOMRuntimePath
  14.  
  15. #SPO Client Object Model Context
  16. $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
  17. $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)
  18. #$spoCredentials = New-Object System.Net.NetworkCredential($sUserName, $sPassword) # for on premise
  19. $spoCtx.Credentials = $spoCredentials
  20.  
  21. $web = $spoCtx.Web
  22. $spGroups =$web.SiteGroups
  23.  
  24. $spoCtx.Load($web)
  25. $spoCtx.Load($spGroups)
  26. $spoCtx.ExecuteQuery()
  27.  
  28. Write-Host "Groups Count= $($spGroups.Count)"
  29.  
  30. foreach($spGroup in $spGroups)
  31. {
  32. $site =@()
  33. $users = $spGroup.Users
  34. $spoCtx.Load($spGroup)
  35. $spoCtx.Load($users)
  36. $spoCtx.ExecuteQuery()
  37.  
  38. foreach($spUser in $users)
  39. {
  40. try
  41. {
  42. $spoCtx.Load($spUser)
  43. $spoCtx.ExecuteQuery()
  44.  
  45.  
  46.  
  47. $site += New-Object -TypeName PSObject -Property @{
  48. SiteUrl =$sSiteUrl
  49. GroupName = $spGroup.Title
  50. PrincipalType= $spGroup.Roles
  51. User =$spUser.LoginName
  52. UserEmail =$spUser.Email
  53. } | Select SiteUrl,GroupName,PrincipalType,User,UserEmail
  54.  
  55.  
  56. Write-Host "This is test" $.Roles
  57.  
  58. }
  59. catch [System.Exception]
  60. {
  61. Write-Host -ForegroundColor Red $_.Exception.ToString()
  62. }
  63.  
  64.  
  65. }
  66. Write-Host $sCSOMPath
  67.  
  68. # $site| Export-CSV ($sCSOMPath+""+$FileName+".csv") -NoTypeInformation -Append #-Encoding UTF8
  69.  
  70. }
  71.  
  72. Read-Host -Prompt "file created Successfull..! in the following path $sCSOMPath, Press any key to close this window"
  73.  
  74. }
  75. catch [System.Exception]
  76. {
  77. Write-Host -ForegroundColor Red $_.Exception.ToString()
  78. Read-Host -Prompt "Operation failed..! Press any key to close this and re run the script"
  79. }
  80. }
  81.  
  82. $FileName= "SiteCollection_Group_Members"
  83. $sSiteUrl = "https://mysite.test.com"
  84. $sUserName = "admin@micrososft.com"
  85. $sPassword = ConvertTo-SecureString "Password" -AsPlainText -Force
  86.  
  87. $scriptpath = $MyInvocation.MyCommand.Path
  88. $dir = Split-Path $scriptpath
  89.  
  90. Write-Host "Getting Site Collection group Members"
  91. Write-Host "-----------------------------------"
  92. Get-SiteGroup-Owners -sCSOMPath $dir -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -FileName $FileName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement