-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathIdentify-SharePoint-Online-Permissions.ps1
149 lines (119 loc) · 6.2 KB
/
Identify-SharePoint-Online-Permissions.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.Online.SharePoint.Client.Tenant.dll"
function Get-SPOTenantSiteCollections
{
param ($sSiteUrl,$sUserName,$sPassword)
try
{
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
Write-Host "Getting the Tenant Site Collections and start Invertory" -foregroundcolor Green
Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
#SPO Client Object Model Context
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)
$spoCtx.Credentials = $spoCredentials
$spoTenant= New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($spoCtx)
$spoTenantSiteCollections=$spoTenant.GetSiteProperties(0,$true)
$spoCtx.Load($spoTenantSiteCollections)
$spoCtx.ExecuteQuery()
#We need to iterate through the $spoTenantSiteCollections object to get the information of each individual Site Collection
foreach($spoSiteCollection in $spoTenantSiteCollections){
#Write-Host "Url: " $spoSiteCollection.Url " - Template: " $spoSiteCollection.Template " - Owner: " $spoSiteCollection.Owner
Get-SPOAllSitePermisions $spoSiteCollection.Url $sUserName $sPassword
}
$spoCtx.Dispose()
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}
Function Get-SPOAllSitePermisions ($url,$admin,$pass)
{
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin, $pass)
$web = $ctx.Web
Load-CSOMProperties -Object $web -PropertyNames @("HasUniqueRoleAssignments", "Url", "Title")
$ctx.Load($ctx.Web.Webs)
$ctx.Load($ctx.Web.RoleAssignments)
$ctx.ExecuteQuery()
Write-Host $web.Url
$webUrl = $web.Url
if($web.HasUniqueRoleAssignments -eq $true) {
$firstIteration = $true #helps when to append commas
foreach($roleAssignment in $ctx.Web.RoleAssignments) {
Load-CSOMProperties -Object $roleAssignment -PropertyNames @("Member","RoleDefinitionBindings")
$ctx.ExecuteQuery()
$roles = ($roleAssignment.RoleDefinitionBindings | Select -ExpandProperty Name) -join ", ";
if($roleAssignment.Member.PrincipalType -eq "User")
{
$web.Title + "," + $web.Url + "," + "N/A" + "," + $roleAssignment.Member.LoginName + "," + $roles + "," + $web.HasUniqueRoleAssignments | out-file $OutputFile -append
}
}
}
$lists=$web.Lists;
$ctx.Load($ctx.Web.Lists);
$ctx.ExecuteQuery();
# write-host "List count is" $lists.Count
foreach($list in $lists)
{
Load-CSOMProperties -Object $list -PropertyNames @("HasUniqueRoleAssignments", "Title")
$ctx.Load($list)
$ctx.Load($list.RoleAssignments)
$ctx.ExecuteQuery()
if($list.HasUniqueRoleAssignments -eq $true) {
$firstIteration = $true #helps when to append commas
foreach($roleAssignment in $list.RoleAssignments) {
Load-CSOMProperties -Object $roleAssignment -PropertyNames @("Member","RoleDefinitionBindings")
$ctx.ExecuteQuery()
$roles = ($roleAssignment.RoleDefinitionBindings | Select -ExpandProperty Name) -join ", ";
write-host $roleAssignment.Member.LoginName
if($roleAssignment.Member.PrincipalType -eq "User")
{
#write-host $list.Title $roles
$web.Title + "," + $web.Url + "," + $list.Title + "," + $roleAssignment.Member.LoginName + "," + $roles | out-file $OutputFile -append
}
}
$listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ctx.load($listItems)
$ctx.executeQuery()
foreach($listItem in $listItems)
{
Load-CSOMProperties -Object $listItem -PropertyNames @("HasUniqueRoleAssignments")
$ctx.Load($listItem)
$ctx.Load($listItem.RoleAssignments)
$ctx.ExecuteQuery()
if($listItem.HasUniqueRoleAssignments -eq $true) {
$firstIteration = $true #helps when to append commas
foreach($roleAssignment in $listItem.RoleAssignments) {
Load-CSOMProperties -Object $roleAssignment -PropertyNames @("Member","RoleDefinitionBindings")
$ctx.ExecuteQuery()
$roles = ($roleAssignment.RoleDefinitionBindings | Select -ExpandProperty Name) -join ", ";
write-host $roleAssignment.Member.PrincipalType
if($roleAssignment.Member.PrincipalType -eq "User") {
#Write-Host "ID - " $listItem["ID"] "Title - " $listItem["Title"] $roles
$web.Title + "," + $web.Url + "," + $list.Title + "," +$listItem.Title + ","+ $roleAssignment.Member.LoginName + "," + $roles | out-file $OutputFile -append
}
}
}
}
}
}
if($web.Webs.Count -eq 0)
{
}
else {
foreach ($web in $web.Webs) {
Get-SPOAllSitePermisions -Url $web.Url
}
}
}
#Required Parameters
$sSiteUrl = ""
$sUserName = ""
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString
$sPassword=convertto-securestring "Password" -asplaintext -force
$OutputFile = "C:\Powershell\AllSitePermissions.csv"
"Web Title,Web URL,List Title,User or Group,Role,Inherited" | out-file $OutPutfile
Get-SPOTenantSiteCollections -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword