Mile T.
Mile T.

Reputation: 47

PowerShell to Export SharePoint Sites and Subsides with security groups

I'm attempting to come up with a powershell script that would allow me to export a list of sites and subsites and the permission groups there in to a CSV.

I'm familiar with using the cmdlts but not building whole scripts. I'm able to use:

Get-SPOSiteGroup  | Export-CSV C:\...

To export site groups to a CSV but it doesn't include the name of the sites they are in.

I also found a script online that would print out the sites and subsite in my site collection here: https://sharepoint.stackexchange.com/questions/101176/powershell-to-list-all-sites-and-subsites-in-sharepoint-online

I'm not sure how to marry the information. I'm trying to export to a CSV a list of sites and subsites and the security groups there in.

I try to run:

get-sposite | Get-SPOSiteGroup **webdite**

And get this error message:

"Get-SPOSiteGroup : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input"

I'm not sure how to get all of this to work together.

Upvotes: 0

Views: 3482

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59338

Get-SPOSiteGroup cmdlet accepts Site parameter, so site groups per every site collection within a tenant could be retrieved like this:

Connect-SPOService -Url $adminUrl 
$sitesInfo = Get-SPOSite 

#Retrieve and print all sites
foreach ($site in $sitesInfo) {
    Write-Host 'Site collection:' $site.Url     
    Get-SPOSiteGroup -Site $site.Url
    Write-Host '-----------------------------' 
}    

To retrieve in addition groups per every web site, the following script could be utilized:

$adminUrl = "https://<tenant>-admin.sharepoint.com"
$UserName = "<username>@<tenant>.onmicrosoft.com" 
$Password = "--password goes here--"

$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$pscreds = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $SecurePassword

Connect-SPOService -Url $adminUrl -Credential $pscreds
$sitesInfo = Get-SPOSite 

foreach ($site in $sitesInfo) {
    Write-Host 'Site collection:' $site.Url     
    #1. Retrieve and print site info 
    #Get-SPOSiteGroup -Site $site.Url
    $AllWebs = Get-SPOWebs -Url $site.Url -Credential $creds

    #2.Retrieve and print webs info (groups Title property in this case)
    foreach ($web in $AllWebs) {
        $web.Context.Load($web.RoleAssignments.Groups)
        $web.Context.ExecuteQuery()
        $web.RoleAssignments.Groups.GetEnumerator() | % { Write-Host $_.Title }   
    }
    Write-Host '-----------------------------' 
}

Key points:

  1. to retrieve webs within a site collection Get-SPOWebs.ps1 is utilized here
  2. to get groups per web site Web.RoleAssignments.Groups is used

Upvotes: 1

Related Questions