-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStart-DAContainerManagement.ps1
More file actions
265 lines (225 loc) · 9.64 KB
/
Start-DAContainerManagement.ps1
File metadata and controls
265 lines (225 loc) · 9.64 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<#
.SYNOPSIS
Script is used to rebalance Site Collections across multiple DocAve containers
.DESCRIPTION
This script will be used to check the total amount of Site Collections within a container.
In large environments we recommend to not put in more than 15.000 Site Collections into
one container to avoid performance problems.
.EXAMPLE
$cred = get-Credential
Start-DAContainerManagement -SourceContainerNameToCheck "Container to balance" -ContainerPrefix "SPO" -AppProfile "SPOProfile" -ControlHost "DocAveServer" -Username $cred.username -Password $cred.password
.COMPONENT
AvePoint Cloud Records
#>
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
SupportsShouldProcess=$true,
PositionalBinding=$false,
HelpUri = 'http://www.avepoint.com.com/',
ConfirmImpact='Medium')]
param (
# Provide Source container name to check against
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[String]
$SourceContainerNameToCheck,
#Define Container pre fix name. Date will be added to it when creating containers
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]
$ContainerPreFix,
# App Profile to use to connect to SharePoint Site
[Parameter(Mandatory=$true)]
[string]
$AppProfile,
#DocAve Control Host Server hostname
[Parameter(Mandatory=$true)]
[string]
$ControlHost,
#DocAve Server username with administrative permissions
[Parameter(Mandatory=$true)]
[string]
$UserName,
#DocAve Server password with administrative permissions
[Parameter(Mandatory=$true)]
[SecureString]
$Password,
#DocAve Control Server Port
[Parameter(Mandatory=$false)]
[string]
$ControlPort = "14000",
# Optional - DocAve Agent Group to use, otherwise default group
[Parameter(Mandatory=$false)]
[string]
$AgentGroup = 'DEFAULT_SHAREPOINT_SITES_AGENT_GROUP',
#Define maximum number of Site Collections in container
[Parameter(Mandatory=$false)]
[int]
$ContainerSize = 15000,
# Optional - Log Directory
[Parameter(Mandatory=$false)]
[string]
$LogDir = 'Logs'
)
# V 1.0 06/22/2021 Initial Release
# V 1.1 09/02/2021 New: Optional parameter $LogDir
# V 1.2 11/12/2021 New: Output of Logfile will be formatted as UTF8
# V 1.3 12/02/2021 New: Exclude tenant-my.sharepoint.com OneDrive Root.
$currentTime=Get-Date -Format 'yyyy-MM-dd_HH-mm-ss';
$LogFileName = "Start-DAContainerManagement"+$currentTime+".log";
$logName = Join-Path -Path $LogDir -ChildPath $LogFileName;
# Requires DocAve PowerShell to be installed on the systems running this script
# Import DocAve Module / PowerShell
import-module DocAveModule -ErrorAction Stop
function Main {
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
SupportsShouldProcess=$true,
PositionalBinding=$false,
HelpUri = 'http://www.avepoint.com.com/',
ConfirmImpact='Medium')]
Param ()
begin {
CreateLogPath;
OutputToHostAndLog "---- Start script ----"
}
process {
if ($pscmdlet.ShouldProcess("Target", "Operation")) {
### Login to the control service
try {
Login-DAManager -ControlHost $ControlHost -ControlPort $ControlPort -Username $UserName -Password $Password
}
catch {
OutputToHostAndLog "Login to DocAve failed $($Error[0])"
break
}
try {
$SourceContainer = Get-DASPOnlineSitesGroup -Name $SourceContainerNameToCheck
OutputToHostAndLog "Source Site Collection count: [$($SourceContainer.Sites.Count)] in Container [$($SourceContainerNameToCheck)]"
}
catch {
OutputToHostAndLog "No DocAve Source Container with name [$($SourceContainer.Sites.Count)] found"
break
}
If ($SourceContainer.Sites.Count -ge 1)
{
OutputToHostAndLog 'More than one Site Collection found in source container.'
#Try to move into existing containers
foreach ($Site in $SourceContainer.Sites)
{
#New: Don't touch MySite Host, this is always part of SPO group and would result in an error
If(!$Site.URL.EndsWith("-my.sharepoint.com"))
{
OutputToHostAndLog 'Load containers'
#Performance might be better if running next line outside of this loop
#But needs to determine amount of sites available within container to stay within the limits of 15.000
$Containers = Get-DASPOnlineSitesGroup
OutputToHostAndLog "Containers count: $($Containers.count)"
#Rebalance into new Container
#Find a container with remaining capacity
$ContainerFound = $false
foreach ($Container in $Containers) {
If(($Container.Sites.Count -le $ContainerSize) -and ($Container.Name -ne $SourceContainer.Name) -and ($Container.GroupType -eq $SourceContainer.GroupType) -and ($ContainerFound -eq $false) -and ($Container.Name -like "$ContainerPreFix*" ))
{
#Determine size of container
#$i = $Container.Count - $ContainerSize
#Add Site Collections
Remove-DASiteFromContainer -ContainerName $SourceContainer.Name -SiteCollectionURL $Site.URL
Add-DASiteToContainer -ContainerName $Container.Name -SiteCollectionURL $Site.URL -AppManagementProfile $AppProfile
$ContainerFound = $true
}
}
If ($ContainerFound -eq $false) {
#CreateContainer and put Site Collection into it
$TodaysDate = Get-Date -Format 'yyyy-MM-dd hh:mm'
$NewContainerName = "$($ContainerPreFix)_$($TodaysDate)"
OutputToHostAndLog "Create container [$($NewContainerName)]"
$CreatedContainer = New-DAContainer -ContainerName $NewContainerName -GroupType $SourceContainer.GroupType
#Add Site Collections
Remove-DASiteFromContainer -ContainerName $SourceContainer.Name -SiteCollectionURL $Site.URL
Add-DASiteToContainer -ContainerName $CreatedContainer.Name -SiteCollectionURL $Site.URL -AppManagementProfile $AppProfile
}
}
}
}
}
}
end {
OutputToHostAndLog "---- End script ----"
}
}
function New-DAContainer {
param (
$ContainerName,
$GroupType
)
$NewContainer = Get-DABlankSPOnlineSitesGroup
$NewContainer.Name = $ContainerName
$NewContainer.Description = "Dynamic Container to hold not more than $($ContainerSize) Site Collections"
$NewContainer.AgentGroupName = $AgentGroup
$NewContainer.GroupType = $GroupType
OutputToHostAndLog "Create new container [$($NewContainer)] [$($NewContainer.AgentGroupName)] [$($NewContainer.GroupType = $GroupType)]"
try {
New-DASPOnlineSitesGroup -SitesGroup $NewContainer
}
catch {
Write-Host ("An error occurred. [$($NewContainer)]`n" + "$($_.Exception.Message)");
OutputToLog ("An error occurred. [$($NewContainer)]`n" + "$($_.Exception.ToString())`n$($_.ScriptStackTrace)")
}
return $NewContainer
}
function Add-DASiteToContainer {
param (
$ContainerName,
$SiteCollectionURL,
$AppManagementProfile
)
#Add Site Collection
$DocAveContainer = Get-DASPOnlineSitesGroup -Name $ContainerName
$site = $DocAveContainer.GetBlankSiteCollectionConfiguration()
$site.Url = $SiteCollectionURL
$site.SetAppManagementProfile($AppManagementProfile)
OutputToHostAndLog "Add Site Collection to Container [$($site.url)] [$($DocAveContainer.Name)]"
try {
$DocAveContainer.AddSiteCollection($site)
}
catch {
Write-Host ("An error occurred. [$($site.Url)]`n" + "$($_.Exception.Message)");
OutputToLog ("An error occurred. [$($site.Url)]`n" + "$($_.Exception.ToString())`n$($_.ScriptStackTrace)")
}
}
function Remove-DASiteFromContainer {
param (
$ContainerName,
$SiteCollectionURL
)
#Add Site Collection
$DocAveContainer = Get-DASPOnlineSitesGroup -Name $ContainerName
OutputToHostAndLog "Remove Site Collection from Container [$($SiteCollectionURL)] [$($DocAveContainer.Name)]"
try {
$DocAveContainer.DeleteSiteCollection($SiteCollectionURL)
}
catch {
Write-Host ("An error occurred. [$($SiteCollectionURL)]`n" + "$($_.Exception.Message)");
OutputToLog ("An error occurred. [$($SiteCollectionURL)]`n" + "$($_.Exception.ToString())`n$($_.ScriptStackTrace)")
}
}
function CreateLogPath()
{
if(!(Test-Path $script:LogDir))
{
mkdir $script:LogDir | Out-Null
}
}
function OutputToHostAndLog($str)
{
Write-Host $str;
OutputToLog $str;
}
function OutputToLog($str)
{
$dateTime=Get-Date -Format 'yyyy-MM-dd HH:mm:ss';
$message=$dateTime+" "+$str;
$message | Out-File -Append -filepath $logName -Encoding utf8;
}
Main