-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathInvoke-ADConnect.ps1
147 lines (109 loc) · 4.82 KB
/
Invoke-ADConnect.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
<#
.SYNOPSIS
This function invokes AD Connect to sync the user if credentials were provided.
.DESCRIPTION
This function invokes AD Connect to sync the user if credentials were provided.
.PARAMETER PowershellSessionName
This is the name of the powershell session that will be used to trigger ad connect.
.OUTPUTS
Powershell session to use for aad connect commands.
.EXAMPLE
invoke-adConnect -powerShellSessionName NAME
#>
Function Invoke-ADConnect
{
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true)]
$PowershellSessionName,
[Parameter(Mandatory = $false)]
$isSingleAttempt = $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.
$workingPowershellSession=$null
$invokeTest=$null
$invokeSleep=$false
#Start function processing.
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "BEGIN INVOKE-ADCONNECT"
Out-LogFile -string "********************************************************************************"
#Obtain the powershell session to work with.
try
{
$workingPowershellSession = Get-PSSession -Name $PowershellSessionName
out-logfile -string $workingPowershellSession
}
catch
{
Out-LogFile -string $_ -isError:$TRUE
}
#Using the powershell session import the ad connect module.
try
{
out-logfile -string "Importing ADSync module..."
invoke-command -session $workingPowershellSession -ScriptBlock {Import-Module -Name 'AdSync'} *>&1
}
catch
{
Out-LogFile -string
}
#Establisha a retry counter.
#The script will try to trigger ad connect 10 times - if not successful move on.
#Eventually AD conn\ect will run on it's own or potentially there is an issue with the remote powershell session <or> the server itself.
if ($isSingleAttempt -eq $FALSE)
{
$doCounter=0
do
{
if ($invokeSleep -eq $TRUE)
{
start-sleepProgress -sleepString "Retrying after waiting 30 seconds." -sleepSeconds 30
}
else
{
out-logfile -string "This is first attempt - skipping sleep."
$invokeSleep = $true
}
$invokeTest = Invoke-Command -Session $workingPowershellSession -ScriptBlock {start-adsyncsynccycle -policyType 'Delta'} *>&1
out-logfile -string $invokeTest
if ($invokeTest.result -ne "Success")
{
out-logFile -string "An error has occurred - this is not necessarily uncommon."
out-logFile -string $invokeTest.exception.toString()
}
else
{
out-logfile -string "The results of the AD Sync."
out-logfile -string $invokeTest.result
}
$doCounter=$doCounter+1
out-logfile ("Retry counter incremented: "+$doCounter.tostring())
} until (($invokeTest.result -eq "Success") -or ($doCounter -eq 10))
}
else
{
out-logfile -string "Attempting one time invocation of AD Connect for multi-threaded retry."
$invokeTest = Invoke-Command -Session $workingPowershellSession -ScriptBlock {start-adsyncsynccycle -policyType 'Delta' -errorAction Continue} *>&1
out-logfile -string $invokeTest
if ($invokeTest.result -ne "Success")
{
out-logFile -string "An error has occurred - this is not necessarily uncommon."
out-logFile -string $invokeTest.exception.toString()
}
else {
out-logfile -string "The results of the AD Sync."
out-logfile -string $invokeTest.result
}
}
if ($doCounter -eq 10)
{
out-logfile -string "AD Connect was not triggered due to retry limit reached."
out-logfile -string "Consider reviewing the AD Connect server for any potential issues."
}
Out-LogFile -string "Function to trigger AD Connect."
Out-LogFile -string "END INVOKE-ADCONNECT"
Out-LogFile -string "********************************************************************************"
}