forked from pankajsurti/JiveSoftware2SPOMigration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncSPO2Office365Group.ps1
More file actions
235 lines (196 loc) · 8.4 KB
/
SyncSPO2Office365Group.ps1
File metadata and controls
235 lines (196 loc) · 8.4 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
$tenantName = "TODO YOUR TENANT"
$spoAdminCenterURL = $("https://{0}-admin.sharepoint.com" -f $tenantName)
$connAdmin = Connect-PnPOnline -Url $spoAdminCenterURL -ReturnConnection -UseWebLogin
$connGraph = Connect-PnPOnline -Scopes "Group.ReadWrite.All", "Directory.ReadWrite.All" -Url https://dvagov-admin.sharepoint.com -ReturnConnection -UseWebLogin
$o365groupnameArray = @()
Import-Csv $PSScriptRoot"\o365GroupNames.csv" | ForEach-Object {
$o365groupnameArray += $_.o365groupname
}
function Write-Log
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("LogContent")]
[string]$Message,
[Parameter(Mandatory=$false)]
[Alias('LogPath')]
[string]$Path='C:\Logs\PowerShellLog.log',
[Parameter(Mandatory=$false)]
[ValidateSet("Error","Warn","Info")]
[string]$Level="Info",
[Parameter(Mandatory=$false)]
[switch]$NoClobber
)
Begin
{
# Set VerbosePreference to Continue so that verbose messages are displayed.
$VerbosePreference = 'Continue'
}
Process
{
# If the file already exists and NoClobber was specified, do not write to the log.
if ((Test-Path $Path) -AND $NoClobber) {
Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
Return
}
# If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
elseif (!(Test-Path $Path)) {
Write-Verbose "Creating $Path."
$NewLogFile = New-Item $Path -Force -ItemType File
}
else {
# Nothing to see here yet.
}
# Format Date for our Log File
$FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Write message to error, warning, or verbose pipeline and specify $LevelText
switch ($Level) {
'Error' {
Write-Error $Message
$LevelText = 'ERROR:'
}
'Warn' {
Write-Warning $Message
$LevelText = 'WARNING:'
}
'Info' {
Write-Verbose $Message
$LevelText = 'INFO:'
}
}
# Write log entry to $Path
"$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append
## also dump to console
#$savedColor = $host.UI.RawUI.ForegroundColor
#$host.UI.RawUI.ForegroundColor = "DarkGreen"
Write-Output $message
#Write-Host $message
#$host.UI.RawUI.ForegroundColor = $savedColor
}
End
{
}
}
$LogFolderPath = $("{0}\LogFiles\{1}" -f $psScriptRoot, (Get-Date -Format "MM-dd-yy"))
$LogFileName = $("{0}\Log-{1}-{2}.txt" -f $LogFolderPath, $sitePlaceid , (Get-Date -Format "HH_mm_ss"))
if( -not ( Test-Path -Path $LogFolderPath ) )
{
# create directory
New-Item -ItemType Directory -Force -Path $LogFolderPath
}
foreach ($o365groupname in $o365groupnameArray)
{
try
{
$grpFound = Get-PnPUnifiedGroup -Identity $o365groupname
if ( $grpFound -ne $null )
{
Write-Log -Path $LogFileName $("{0} group is found" -f $o365groupname)
$ownersUserList = Get-PnPUnifiedGroupOwners -Identity $o365groupname
$membersUsersList = Get-PnPUnifiedGroupMembers -Identity $o365groupname
$siteURL = $("https://{0}.sharepoint.com/sites/{1}" -f $tenantName, $o365groupname)
Write-Log -Path $LogFileName $("siteURL: {0} " -f $siteURL)
$parentConn = Connect-PnPOnline -ReturnConnection -Url $siteURL -UseWebLogin
$parentWebUrl = "/sites/"+$o365groupname
$currentWeb = Get-PnPWeb $parentWebUrl -Connection $parentConn
$ownersgroupname = $currentWeb.Title +' Owners'
$membersgroupname = $currentWeb.Title +' Members'
$Ownersgroup = Get-PnPGroup -Identity $ownersgroupname -Connection $parentConn -Web $currentWeb
$membersgroup = Get-PnPGroup -Identity $membersgroupname -Connection $parentConn -Web $currentWeb
$spoOwnersUserList = Get-PnPGroupMembers -Identity $ownersgroup
$spoMembersUserList = Get-PnPGroupMembers -Identity $membersgroup
#get email array for existing owners in office 365 group
$existingOwners = New-Object System.Collections.Generic.List[System.String]
foreach ( $upn in $ownersUserList)
{
if ( $upn.UserPrincipalName.Contains("@contoso.com") -eq $true )
{
$existingOwners.Add($upn.UserPrincipalName)
}
}
#get email array for existing members in office 365 group
$existingMembers = New-Object System.Collections.Generic.List[System.String]
foreach ( $upn in $membersUsersList)
{
if ( $upn.UserPrincipalName.Contains("@contoso.com") -eq $true )
{
$existingMembers.Add($upn.UserPrincipalName)
}
}
$owners2Add = New-Object System.Collections.Generic.List[System.String]
foreach ( $spoOwnerItem in $spoOwnersUserList)
{
# check is the user in SPO owner has contoso.com and it is alredy not a o365 owner
if( ( $spoOwnerItem.Email.Contains("@contoso.com") -eq $true) -and
( $existingOwners.Contains($spoOwnerItem.Email) -eq $false ) )
{
$owners2Add += $spoOwnerItem.Email
}
}
$members2Add = New-Object System.Collections.Generic.List[System.String]
foreach ( $spoMemberItem in $spoMembersUserList)
{
# check is the user in SPO owner has contoso.com and it is alredy not a o365 owner
if( ( $spoMemberItem.Email.Contains("@contoso.com") -eq $true) -and
( $existingMembers.Contains($spoMemberItem.Email) -eq $false ) )
{
$members2Add += $spoMemberItem.Email
}
}
Write-Log -Path $LogFileName $("O365 Group's owners = {0}" -f $existingOwners.Count)
Write-Log -Path $LogFileName $("O365 Group's members = {0}" -f $existingMembers.Count)
Write-Log -Path $LogFileName $("SPO Group's owners = {0}" -f $spoOwnersUserList.Count)
Write-Log -Path $LogFileName $("SPO Group's members = {0}" -f $spoMembersUserList.Count)
if ( $owners2Add.Count -gt 0 )
{
Write-Log -Path $LogFileName $("Total new owners2Add.Count = {0}" -f $owners2Add.Count)
$owners2Add += $existingOwners
Write-Log -Path $LogFileName $("Total including existing and new owners2Add.Count = {0}" -f $owners2Add.Count)
try
{
Set-PnPUnifiedGroup -Identity $o365groupname -Owners $owners2Add
}
catch
{
Write-Log -Path $LogFileName $_.Exception
}
}
else
{
Write-Log -Path $LogFileName "Owners are up to date."
}
if ( $members2Add.Count -gt 0 )
{
Write-Log -Path $LogFileName $("Total new members2Add.Count = {0}" -f $members2Add.Count)
$members2Add += $existingMembers
Write-Log -Path $LogFileName $("Total including existing and new members2Add.Count = {0}" -f $members2Add.Count)
try
{
Set-PnPUnifiedGroup -Identity $o365groupname -Members $members2Add
}
catch
{
Write-Log -Path $LogFileName $_.Exception
}
}
else
{
Write-Log -Path $LogFileName "Members are up to date."
}
}
else
{
Write-Log -Path $LogFileName $("{0} group is not found" -f $o365groupname)
}
}
catch
{
Write-Log -Path $LogFileName $("{0} group exception" -f $o365groupname)
Write-Log -Path $LogFileName $_.Exception
}
}