FIM 2010 R2 PowerShell Connector: Flat File Sample Connector
Tip |
---|
For feedback, click here. |
Updated content is now hosted at https://github.com/Microsoft/MIMPowerShellConnectors
Configuration
To create the connector described in this section, configure the tabs in the Management Agent Designer as described in the Windows PowerShell Connector for FIM 2010 R2 Technical Reference.
Template File
The sample connector uses a template file to provide schema information to the Synchronization Service. Create a text file in the Extension folder called SampleInputFile.txt.
By default, the Extensions folder can be found at:
%ProgramFiles%\Microsoft Forefront Identity Manager\2010\Synchronization Service\Extensions\.
Paste the following data in to SampleInputFile.txt.
EmployeeID;OfficePhone;MobilePhone;FaxPhone
100;425-555-0100;206-555-0101;
101;425-555-0120;206-555-0105;312-555-0151
102;425-555-0130;;312-555-0171
103;425-555-0125;;
104;425-555-0127;206-555-0115;312-555-0170
105;425-555-0119;;312-555-0101
11;425-555-0199;206-555-0175;312-555-0194
Connectivity
Parameter | Value |
---|---|
Server | <blank> |
Domain | <blank> |
User | <blank> |
Password | <blank> |
Impersonate Connector Account | Unchecked |
Load User Profile When Impersonating | Unchecked |
Logon Type When Impersonating | None |
Signed Scripts Only | Unchecked |
Common Module Script Name (with extension) | xADSyncPSConnectorModule.psm1 |
Common Module Script | Paste AD Sync PS Connector Module code as value |
Validation Script | <blank> |
Schema Script | Paste GetSchema code as value |
Additional Config Parameter Names | FileName,Delimiter,Encoding |
Additional Encrypted Config Parameter Names | <blank> |
Capabilities
Parameter | Value |
---|---|
Distinguished Name Style | None |
Export Type | ObjectReplace |
Data Normalization | None |
Object Confirmation | Normal |
Use DN as Anchor | Unchecked |
Concurrent Operations of Several Connectors | Checked |
Partitions | Unchecked |
Hierarchy | Unchecked |
Enable Import | Checked |
Enable Delta Import | Unchecked |
Enable Export | Checked |
Enable Full Export | Checked |
No Reference Values In First Export Pass | Unchecked |
Enable Object Rename | Unchecked |
Delete-Add As Replace | Checked |
Enable Password Operations | Unchecked |
Enable Export Password in First Pass | Checked |
Global Parameters
Parameter | Value |
---|---|
Partition Script | <blank> |
Hierarchy Script | <blank> |
Begin Import Script | <blank> |
Import Script | Paste ImportData code as value |
End Import Script | <blank> |
Begin Export Script | <blank> |
Export Script | Paste ExportData code as value |
End Export Script | <blank> |
Begin Password Script | <blank> |
Password Extension Script | <blank> |
End Password Script | <blank> |
FileName_Global | InputFile.txt |
Delimiter_Global | ; |
Encoding_Global | <blank> |
Configure Partitions and Hierarchies
Keep the defaults as listed below.
Select Object Types
Select the Row Object Type as shown below.
Select Attributes
Select each of the four attributes (EmployeeID, FaxPhone, MobilePhone, OfficePhone) as shown below.
Configure Anchors
Specify the EmployeeID attribute as the Anchor attribute for the Row Object Type as shown below.
Run Profiles
Once the connector has been created, create a run profile with a Full Import run step. Create an additional run profile with a Full Export run step.
Testing
Create a text file called InputFile.txt in the connector’s MAData folder, and then paste the following sample data into it:
EmployeeID;OfficePhone;MobilePhone;FaxPhone
100;425-555-0100;206-555-0101;
101;425-555-0120;206-555-0105;312-555-0151
102;425-555-0130;;312-555-0171
103;425-555-0125;;
104;425-555-0127;206-555-0115;312-555-0170
105;425-555-0119;;312-555-0101
11;425-555-0199;206-555-0175;312-555-0194
By default, the MAData folder can be found at:
%ProgramFiles%\Microsoft Forefront Identity Manager\2010\Synchronization Service\MAData\
Next, run the Full Import run profile for the connector. If successful, the results should look similar to the following screen capture.
Troubleshooting
The Windows PowerShell connector supports logging and tracing of connector and script activities for troubleshooting purposes.
Critical errors will be logged to the Application log on the Synchronization Service server and additional tracing can be enabled by the administrator.
Event Log
When a fatal error occurs while the PowerShell connector is running, or during the configuration of the connector in the Management Agent Designer, the Synchronization Service will log an event with the following details:
Log | Application |
Level | Error |
Source | FIMSynchronizationService |
Event ID | 6801 |
Task Category | Server |
Logging/Tracing
The Windows PowerShell connector can be configured to emit tracing information to any .NET TraceListener (e.g. event log, XML file, text file, etc.).
Administrators can also configure the connector to log information produced by the Write-Warning, Write-Verbose, and Write-Debug cmdlets to the trace log.
To enable tracing, follow these steps:
Open the %ProgramFiles%\Microsoft Forefront Identity Manager\2010\Synchronization Service\bin\miiserver.exe.config file using a text editor.
Paste the following XML in to the file on the line immediately following the sources tag.
<source name="ConnectorsLog" switchValue="Verbose">
<listeners>
<add initializeData="C:\Logs\ConnectorsTrace.log"
type="System.Diagnostics.TextWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ConnectorsTraceListener">
<filter type="" />
</add>
</listeners>
</source>
The <System.Diagnostics> section of the miiserver.exe.config file should now resemble the following excerpt:
Create the directory c:\logs.
Grant the Synchronization Service service account Modify permissions to the c:\logs directory.
Restart the Synchronization Service.
Trace Windows PowerShell Script Information
To trace information from Windows PowerShell scripts, you must complete two steps:
- Add logging to your scripts in the form of Write-Verbose, or Write-Debug statements.
- Enable $VerbosePreference or $DebugPreference in the properties of the Run Step as shown below.
PowerShell Configuration Script Code
GetSchema
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 |
param(
[System.Collections.ObjectModel.KeyedCollection[[string], [Microsoft.MetadirectoryServices.ConfigParameter]]] $ConfigParameters, [PSCredential] $PSCredential ) Set-PSDebug -Strict Import-Module (Join-Path -Path ([Environment]::GetEnvironmentVariable('TEMP', [EnvironmentVariableTarget]::Machine)) -ChildPath 'xADSyncPSConnectorModule.psm1') -Verbose:$false $TemplateFile = (Join-Path -Path (Get-xADSyncPSConnectorFolder -Folder Extensions) -ChildPath 'SampleInputFile.txt') $Delimiter = ';' $Encoding = 'Default' if ((Test-Path -Path $TemplateFile -PathType Leaf) -eq $false) { throw "Cannot find template file $TemplateFile" } $TemplateCSV = Import-Csv -Path $TemplateFile -Delimiter $Delimiter -Encoding $Encoding if ($TemplateCSV -eq $null) { throw 'Imported CSV is null' } $Schema = New-xADSyncPSConnectorSchema $SchemaType = New-xADSyncPSConnectorSchemaType -Name 'Row' $Columns = $TemplateCSV[0] | Get-Member -MemberType NoteProperty foreach ($c in $Columns) { $SchemaType | Add-xADSyncPSConnectorSchemaAttribute -Name $c.Name -DataType String -SupportedOperation ImportExport } $Schema.Types.Add($SchemaType) Write-Output $Schema |
ImportData
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 |
param(
[System.Collections.ObjectModel.KeyedCollection[[string], [Microsoft.MetadirectoryServices.ConfigParameter]]] [ValidateNotNull()] $ConfigParameters, [Microsoft.MetadirectoryServices.Schema] [ValidateNotNull()] $Schema, [Microsoft.MetadirectoryServices.OpenImportConnectionRunStep] $OpenImportConnectionRunStep, [Microsoft.MetadirectoryServices.ImportRunStep] $GetImportEntriesRunStep, [PSCredential] $PSCredential ) Set-PSDebug -Strict Import-Module (Join-Path -Path ([Microsoft.MetadirectoryServices.MAUtils]::MAFolder) -ChildPath 'xADSyncPSConnectorModule.psm1') -Verbose:$false $importResults = New-Object -TypeName 'Microsoft.MetadirectoryServices.GetImportEntriesResults' $csEntries = New-Object -TypeName 'System.Collections.Generic.List[Microsoft.MetadirectoryServices.CSEntryChange]' $columnsToImport = $Schema.Types[0].Attributes Write-Verbose "Loaded $($columnsToImport.Count) attributes to import" $importCsvParameters = @{ Path = (Join-Path -Path (Get-xADSyncPSConnectorFolder -Folder ManagementAgent) -ChildPath (Get-xADSyncPSConnectorSetting -Name 'FileName' -Scope Global -ConfigurationParameters $ConfigParameters)) } if ((Test-Path $importCsvParameters['Path'] -PathType Leaf) -eq $false) { ##TODO: ECMA exception? throw "Could not find $($importCsvParameters['Path'])" } Write-Verbose "Import path: $($importCsvParameters['Path'])" $delimiter = Get-xADSyncPSConnectorSetting -Name 'Delimiter' -Scope Global -ConfigurationParameters $ConfigParameters if ($delimiter) { $importCsvParameters.Add('Delimiter', $delimiter) Write-Verbose "Setting delimiter to $delimiter)" } $encoding = Get-xADSyncPSConnectorSetting -Name 'Encoding' -Scope Global -ConfigurationParameters $ConfigParameters if ($encoding) { ##TODO: Validation $importCsvParameters.Add('Encoding', $encoding) Write-Verbose "Setting encoding to $encoding)" } $recordsToImport = Import-Csv @importCsvParameters Write-Verbose "Imported $($recordsToImport.Count) records" foreach ($record in $recordsToImport) { Write-Verbose 'Starting new record' ##TODO: Handle a missing anchor (what exception to throw?) $foundValidColumns = $false $csEntry = New-xADSyncPSConnectorCSEntryChange -ObjectType $Schema.Types[0].Name -ModificationType Add foreach ($column in $columnsToImport) { $columnName = $column.Name Write-Verbose "Processing column $columnName" if ($record.$columnName) { Write-Verbose 'Found column' $foundValidColumns = $true ##TODO: Support multivalue? $csEntry | Add-xADSyncPSConnectorCSAttribute -ModificationType Add -Name $columnName -Value $record.$columnName } } if ($foundValidColumns) { Write-Verbose 'Publishing CSEntryChange' $csEntries.Add($csEntry) } Write-Verbose 'Record completed' } ##TODO: Support paging $importResults.CSEntries = $csEntries $importResults.MoreToImport = $false Write-Output $importResults |
ExportData
001
002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
param(
[System.Collections.ObjectModel.KeyedCollection[[string], [Microsoft.MetadirectoryServices.ConfigParameter]]] $ConfigParameters, [Microsoft.MetadirectoryServices.Schema] $Schema, [Microsoft.MetadirectoryServices.OpenExportConnectionRunStep] $OpenExportConnectionRunStep, [System.Collections.Generic.IList[Microsoft.MetaDirectoryServices.CSEntryChange]] $CSEntries, [PSCredential] $PSCredential ) Set-PSDebug -Strict Import-Module (Join-Path -Path ([Microsoft.MetadirectoryServices.MAUtils]::MAFolder) -ChildPath 'xADSyncPSConnectorModule.psm1') -Verbose:$false function CreateCustomPSObject { param ( $PropertyNames = @() ) $template = New-Object -TypeName System.Object foreach ($property in $PropertyNames) { $template | Add-Member -MemberType NoteProperty -Name $property -Value $null } return $template } $exportCsvParameters = @{ Path = (Join-Path -Path (Get-xADSyncPSConnectorFolder -Folder ManagementAgent) -ChildPath (Get-xADSyncPSConnectorSetting -Name 'FileName' -Scope Global -ConfigurationParameters $ConfigParameters)) } $csentryChangeResults = New-GenericObject System.Collections.Generic.List Microsoft.MetadirectoryServices.CSEntryChangeResult if ((Test-Path ([IO.Path]::GetDirectoryName($exportCsvParameters['Path'])) -PathType Container) -eq $false) { ##TODO: ECMA exception? throw "Could not find $($exportCsvParameters['Path'])" } Write-Verbose "Export path: $($exportCsvParameters['Path'])" $delimiter = Get-xADSyncPSConnectorSetting -Name 'Delimiter' -Scope Global -ConfigurationParameters $ConfigParameters if ($delimiter) { $exportCsvParameters.Add('Delimiter', $delimiter) Write-Verbose "Setting delimiter to $delimiter)" } $encoding = Get-xADSyncPSConnectorSetting -Name 'Encoding' -Scope Global -ConfigurationParameters $ConfigParameters if ($encoding) { ##TODO: Validation $exportCsvParameters.Add('Encoding', $encoding) Write-Verbose "Setting encoding to $encoding)" } $columnsToExport = @() foreach ($attribute in $Schema.Types[0].Attributes) { $columnsToExport += $attribute.Name Write-Verbose "Added attribute $($attribute.Name) to export list" } Write-Verbose "Loaded $($columnsToExport.Count) attributes to export" $csvSource = @() foreach ($entry in $CSEntries) { Write-Verbose "Processing object $($entry.Identifier)" [bool]$objectHasAttributes = $false $baseObject = CreateCustomPSObject -PropertyNames $columnsToExport if ($entry.ModificationType -ne 'Delete') { foreach ($attribute in $columnsToExport) { if (($entry.AttributeChanges.Contains($attribute)) -eq $false -and ($entry.AnchorAttributes.Contains($attribute) -eq $false)) { continue }
if ($entry.AnchorAttributes[$attribute].Value) { $baseObject.$attribute = $entry.AnchorAttributes[$attribute].Value $objectHasAttributes = $true } elseif ($entry.AttributeChanges[$attribute].ValueChanges[0].Value) { $baseObject.$attribute = $entry.AttributeChanges[$attribute].ValueChanges[0].Value $objectHasAttributes = $true } } if ($objectHasAttributes) { $csvSource += $baseObject } $csentryChangeResult = [Microsoft.MetadirectoryServices.CSEntryChangeResult]::Create($entry.Identifier, $null, "Success") } $csentryChangeResults.Add($csentryChangeResult) Write-Verbose "Completed processing object $($entry.Identifier)" } $csvSource | Export-Csv @exportCsvParameters -NoTypeInformation $result = New-Object -TypeName Microsoft.MetadirectoryServices.PutExportEntriesResults $closedType = [Type] "Microsoft.MetadirectoryServices.PutExportEntriesResults" return [Activator]::CreateInstance($closedType, $csentryChangeResults) |