Guest User

Untitled

a guest
Apr 30th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. Function Get-SiteGroup-Members
  2. {
  3. param ($sSiteUrl,$sUserName,$sPassword,$FileName)
  4. try
  5. {
  6. #SPO Client Object Model Context
  7. $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
  8. $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)
  9. #$spoCredentials = New-Object System.Net.NetworkCredential($sUserName, $sPassword) # for on premise
  10. $spoCtx.Credentials = $spoCredentials
  11.  
  12. $web = $spoCtx.Web
  13. $spGroups =$web.SiteGroups
  14.  
  15. $spoCtx.Load($web)
  16. $spoCtx.Load($spGroups)
  17. $spoCtx.ExecuteQuery()
  18.  
  19. Write-Host "Groups Count= $($spGroups.Count)"
  20.  
  21. foreach($spGroup in $spGroups)
  22. {
  23. $site =@()
  24. $users = $spGroup.Users
  25. $spoCtx.Load($spGroup)
  26. $spoCtx.Load($users)
  27. $spoCtx.ExecuteQuery()
  28.  
  29. foreach($spUser in $users)
  30. {
  31. try
  32. {
  33. $spoCtx.Load($spUser)
  34. $spoCtx.ExecuteQuery()
  35.  
  36. $site += New-Object -TypeName PSObject -Property @{
  37. SiteUrl =$sSiteUrl
  38. GroupName = $spGroup.Title
  39. PrincipalType= $spGroup.PrincipalType
  40. User =$spUser.LoginName
  41. UserEmail =$spUser.Email
  42. } | Select SiteUrl,GroupName,PrincipalType,User,UserEmail
  43.  
  44.  
  45. }
  46. catch [System.Exception]
  47. {
  48. Write-Host -ForegroundColor Red $_.Exception.ToString()
  49. }
  50.  
  51.  
  52. }
  53.  
  54. $site| Export-CSV ("C:"+$FileName+".csv") -NoTypeInformation -Append #-Encoding UTF8
  55.  
  56. }
  57.  
  58. Read-Host -Prompt "file created Successfull..! Press any key to close this window"
  59.  
  60. }
  61. catch [System.Exception]
  62. {
  63. Write-Host -ForegroundColor Red $_.Exception.ToString()
  64. Read-Host -Prompt "Operation failed..! Press any key to close this and re run the script"
  65. }
  66. }
  67.  
  68.  
  69. $FileName= "ReportName"
  70. $sSiteUrl = "https://domain.sharepoint.com"
  71. $sUserName = "user@domain.onmicrosoft.com"
  72. $sPassword = ConvertTo-SecureString "YourPassword" -AsPlainText -Force
  73.  
  74.  
  75. Write-Host "Getting Site Collection group Members"
  76. Write-Host "-----------------------------------"
  77. Get-SiteGroup-Members -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -FileName $FileName
Add Comment
Please, Sign In to add comment