forked from timmcmic/DLConversionV2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-ADConnect.ps1
More file actions
118 lines (83 loc) · 3.63 KB
/
Invoke-ADConnect.ps1
File metadata and controls
118 lines (83 loc) · 3.63 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
<#
.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
)
#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
}
catch
{
Out-LogFile -string $_ -isError:$TRUE
}
#Using the powershell session import the ad connect module.
try
{
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 connect will run on it's own or potentially there is an issue with the remote powershell session <or> the server itself.
$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
if ($invokeTest.result -ne "Success")
{
out-logFile -string "An error has occured - this is not necessarily uncommon."
out-logFile -string $invokeTest.exception.toString()
}
$doCounter=$doCounter+1
out-logfile ("Retry counter incremented: "+$doCounter.tostring())
} until (($invokeTest.result -eq "Success") -or ($doCounter -eq 10))
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 "The results of the AD Sync."
out-logfile -string $invokeTest.result
Out-LogFile -string "ADConnect was successfully triggered."
Out-LogFile -string "END INVOKE-ADCONNECT"
Out-LogFile -string "********************************************************************************"
}