-
Notifications
You must be signed in to change notification settings - Fork 10
/
move-toNonSyncOU.ps1
136 lines (103 loc) · 4.93 KB
/
move-toNonSyncOU.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
<#
.SYNOPSIS
This function moves the group to the non-SYNC OU. This is necessary to process the group deletion from Office 365.
.DESCRIPTION
This function moves the group to the non-SYNC OU. This is necessary to process the group deletion from Office 365.
.PARAMETER GlobalCatalogServer
The global catalog to make the query against.
.PARAMETER DN
The original DN of the object.
.PARAMETER OU
This is the OU that is set to not synchonize in AD Connect.
.PARAMETER adCredential
This is the credential for active directory operations.
.OUTPUTS
None
.EXAMPLE
move-toNonSyncOU -globalCatalogServer GC -OU NonSyncOU -DN groupDN -adCredential CRED
#>
Function move-toNonSyncOU
{
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true)]
[string]$globalCatalogServer,
[Parameter(Mandatory = $true)]
$OU,
[Parameter(Mandatory = $true)]
$DN,
[Parameter(Mandatory = $true)]
$adCredential,
[Parameter(Mandatory = $false)]
[ValidateSet("Basic","Negotiate")]
$activeDirectoryAuthenticationMethod="Negotiate",
[Parameter(Mandatory = $false)]
$dlMoveCleanup=$FALSE,
[Parameter(Mandatory = $false)]
$dlPostCreate=$FALSE
)
#Output all parameters bound or unbound and their associated values.
write-functionParameters -keyArray $MyInvocation.MyCommand.Parameters.Keys -parameterArray $PSBoundParameters -variableArray (Get-Variable -Scope Local -ErrorAction Ignore)
#Declare function variables.
#Start function processing.
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "START MOVE-TONONSYNCOU"
Out-LogFile -string "********************************************************************************"
[boolean]$stopLoop=$false
[int]$loopCounter = 0
if ($dlMoveCleanup -eq $FALSE)
{
if ($dlPostCreate -eq $FALSE)
{
do
{
Out-LogFile -string "Move the group to the non-SYNC OU..."
try {
move-adObject -identity $DN -targetPath $OU -credential $adCredential -server $globalCatalogServer -authType $activeDirectoryAuthenticationMethod -errorAction Stop
$stopLoop = $true
}
catch {
if ($loopCounter -lt 5)
{
out-logfile -string "Attempt to move to non-sync OU failed - wait and retry."
out-logfile -string ("Attempt number: "+$loopcounter.tostring())
$loopCounter++
start-sleepProgress -sleepSeconds 5 -sleepString "Attempt to move to non-sync OU failed - sleep 5 seconds retry."
}
else {
out-logfile -string "Unable to move the group to a non-sync OU - abandon the move."
out-logfile -string $_ -isError:$true
}
}
} until ($stopLoop -eq $TRUE)
}
else
{
try {
move-adObject -identity $DN -targetPath $OU -credential $adCredential -server $globalCatalogServer -authType $activeDirectoryAuthenticationMethod -errorAction Stop
}
catch {
out-logfile -string "Unable to move the group between organizational units. Manual intervention required."
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = ""
ExternalDirectoryObjectID = ""
Alias = ""
Name = $DN
Attribute = ""
ErrorMessage = "Unable to move the on premises group between OUs. Manual administrator intervention required."
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$global:postCreateErrors += $isErrorObject
}
}
}
else
{
out-logfile -string "Attempting one move back to the source OU - on premises group was moved to no-sync and failure occurred."
move-adObject -identity $DN -targetPath $OU -credential $adCredential -server $globalCatalogServer -authType $activeDirectoryAuthenticationMethod -errorAction SilentlyContinue
}
Out-LogFile -string "END MOVE-TONONSYNCOU"
Out-LogFile -string "********************************************************************************"
}