-
Notifications
You must be signed in to change notification settings - Fork 36
/
GetSchema.ps1
133 lines (117 loc) · 4.91 KB
/
GetSchema.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
param(
# Table of all configuration parameters set at instance of connector
[System.Collections.ObjectModel.KeyedCollection[[string], [Microsoft.MetadirectoryServices.ConfigParameter]]]
$ConfigParameters,
# Contains any credentials entered by the administrator on the Connectivity tab
[PSCredential]
$PSCredential,
# Filename of Template Input file, please use unique filename for each connector.
[String]
$TemplateInputFile = 'MAName_SampleInputFile.txt',
# Delimiter used in Template Input File
[String]
$Delimiter = ';',
# Encoding used in Template Input File
[String]
$Encoding = 'UTF8',
# Used as objecttype when no complex schema is used.
[String]
$DefaultObjectTypeName = 'Object',
# Path to debug log
[String]
$LogFilePath = "$([System.Environment]::GetEnvironmentVariable('Temp', 'Machine'))\MIMPS_SchemaScript.log"
)
Set-PSDebug -Strict
function Write-Log {
<#
.SYNOPSIS
Function for logging, modify to suit your needs.
#>
[CmdletBinding()]
param([string]$Message, [String]$Path)
# Uncomment this line to enable debug logging
# Out-File -InputObject $Message -FilePath $Path -Append
}
try {
# Remove log if exists:
Remove-Item -Path $LogFilePath -Force -ErrorAction 'Stop'
} catch {
# We don't care about errors here
}
$PSDefaultParameterValues['Write-Log:Path'] = $LogFilePath
try {
$commonModule = Join-Path -Path ([System.Environment]::GetEnvironmentVariable('Temp', 'Machine')) -ChildPath $ConfigParameters['Common Module Script Name (with extension)'].Value
Import-Module -Name $commonModule -Verbose:$false -ErrorAction Stop
Write-Log -Message 'CommonModule imported'
} catch {
throw "Failed to import common module with error [$_]"
}
$FolderPath = Get-xADSyncPSConnectorFolder -Folder 'Extensions'
Write-Log -Message 'ExtensionFolder found'
$TemplateFile = Join-Path -Path $FolderPath -ChildPath $TemplateInputFile
if ((Test-Path -Path $TemplateFile -PathType 'Leaf') -eq $false) {
$Message = "Cannot find template file $TemplateFile"
Write-Log -Message "Cannot find template file $TemplateFile"
throw $Message
}
Write-Log -Message "Importing template input file: $TemplateFile"
$CSVParams = @{
Path = $TemplateFile
Delimiter = $Delimiter
Encoding = $Encoding
}
Write-Log -Message ($CSVParams | ConvertTo-Json)
$TemplateCSV = Import-Csv @CSVParams
Write-Log -Message 'File imported'
if ($null -eq $TemplateCSV) {
throw 'Imported CSV is null'
}
Write-Log -Message 'Creating schema'
$Schema = New-xADSyncPSConnectorSchema
Write-Log -Message 'Schema created'
$SchemaType = $TemplateCSV | Get-Member | Select-Object -ExpandProperty 'TypeName' -First 1
Write-Log -Message "SchemaType: $SchemaType"
if($SchemaType -eq 'CSV:MIMPSConnector.SchemaConfig') {
Write-Log -Message 'Complex Schema'
foreach($SchemaType in $TemplateCSV) {
$ObjectType = $SchemaType | Select-Object -ExpandProperty 'ObjectType'
Write-Log -Message "CreatingType: $ObjectType"
$FileName = $SchemaType | Select-Object -ExpandProperty 'FileName'
$FilePath = Join-Path -Path $FolderPath -ChildPath $FileName
$ReferenceAttributes = @{}
($SchemaType | Select-Object -ExpandProperty 'ReferenceAttributes') -split '\|' | Foreach-Object {
$Key,$Value = $_ -split '#'
$ReferenceAttributes.Add($Key,$Value)
}
$SchemaObjectCSV = Import-Csv -Path $FilePath -Delimiter $Delimiter -Encoding $Encoding
$Columns = $SchemaObjectCSV[0] | Get-Member -MemberType 'NoteProperty'
$SchemaTypeObject = New-xADSyncPSConnectorSchemaType -Name $ObjectType
foreach ($c in $Columns) {
if($c.Name -in $ReferenceAttributes.Keys) {
$DataType = 'Reference'
} else {
$DataType = 'String'
}
Add-xADSyncPSConnectorSchemaAttribute -Name $c.Name -SupportedOperation 'ImportExport' -DataType $DataType -InputObject $SchemaTypeObject
}
Write-Log -Message 'Adding Schematypeobject:'
Write-Log -Message ($SchemaTypeObject|ConvertTo-Json)
$Schema.Types.Add($SchemaTypeObject)
Write-Log -Message 'Object Added'
}
} else {
Write-Log -Message 'Simple Schema'
$Columns = $TemplateCSV[0] | Get-Member -MemberType 'NoteProperty'
Write-Log -Message ($Columns | ConvertTo-Json)
Write-Log -Message $DefaultObjectTypeName
$SchemaTypeObject = New-xADSyncPSConnectorSchemaType -Name $DefaultObjectTypeName
foreach ($c in $Columns) {
Add-xADSyncPSConnectorSchemaAttribute -Name $c.Name -SupportedOperation 'ImportExport' -DataType 'String' -InputObject $SchemaTypeObject
}
Write-Log -Message 'Adding Schematypeobject'
Write-Log -Message ($SchemaTypeObject|ConvertTo-Json)
$Schema.Types.Add($SchemaTypeObject)
Write-Log -Message 'Object Added'
}
Write-Log -Message ($Schema | ConvertTo-Json)
Write-Output $Schema