PARAM ( $Debug = $False, $Verbose = $False, $DNField = "objectDN", $LogFileName = "LDAP-Test" ) . C:\FIM\Scripts\IdentityBroker\LDAP\SharedConfig.ps1 function log($string) { #write-host($string) "$($(Get-Date).ToString("s")): $string" | out-file -Filepath $LogFile -append } log "" log "" log "--------------------------------------------------------------------------------" log "Starting Export Run - Mode: Add" #Needs reference to .NET assembly used in the script. Add-Type -AssemblyName System.DirectoryServices.Protocols try { #Setting credentials recieved from MA $Credentials = New-Object System.Net.NetworkCredential($username,$password) #LDAP Connection log "Opening LDAP Connection" #$LDAPConnection = New-Object System.DirectoryServices.Protocols.LDAPConnection($LDAPServer,$Credentials,"Basic") $LDAPConnection = New-Object System.DirectoryServices.Protocols.LDAPConnection($LDAPServer,$Credentials,"Basic") } catch [System.Exception] { $msg = "An error occurred in opening LDAP connection: $_" log $msg throw $msg } foreach ($entity in $components.InputEntities) { if($Debug) { log "Getting Object DN" } $dn = $null $dnVal = $entity[$DNField] if($dnVal -eq $null) { $msg = "$DNField is a required attribute and is not present" log $msg #$components.Failures.Push($entity) } else { $dn = $dnVal.Value.ToString() log "DN: $dn" } try { $ConvertedAttributes = GetConvertedValues -Entity $entity } catch [System.Exception] { $msg = "An error occurred in converting attributes for LDAP: $_" log $msg #$components.Failures.Push($entity) throw $msg } try { if($Debug) { log "Creating Directory Attribute Collection" log "" } $dirAttrCollection = New-Object System.DirectoryServices.Protocols.DirectoryAttributeCollection foreach($attribute in $ConvertedAttributes.GetEnumerator()) { $directoryAttribute = New-Object System.DirectoryServices.Protocols.DirectoryAttribute $fieldName = $attribute.Name $directoryAttribute.Name = $fieldName if($Debug -and $Verbose) { log "Attribute: $fieldName" } foreach($val in $attribute.Value) { $directoryAttribute.Add($val) if($Debug -and $Verbose) { log "Value: $val" } } $dirAttrCollection.Add($directoryAttribute) } } catch [System.Exception] { $msg = "An error occurred in converting attributes for LDAP Request: $_" log $msg #$components.Failures.Push($entity) throw $msg } if($dirAttrCollection.Count -gt 0) { try { if($Debug) { log "" log "Creating and Sending LDAP Add Request" } $dirAttrArray = New-Object System.DirectoryServices.Protocols.DirectoryAttribute[] $dirAttrCollection.Count $dirAttrCollection.CopyTo($dirAttrArray, 0) $addRequest = New-Object System.DirectoryServices.Protocols.AddRequest($dn, $dirAttrArray) $addResponse = $LDAPConnection.SendRequest($addRequest) if(!$addResponse -eq [System.DirectoryServices.Protocols.ResultCode]::Success) { throw "The LDAP Add Response was not successful" } else { if($Debug) { log "Success" } } } catch [System.Exception] { $msg = "An error occurred in finalising and sending the LDAP Request: $_" log $msg #$components.Failures.Push($entity) throw $msg } } else { if($Debug) { log "No Attributes in LDAP Request" } } if($Debug) { log "" } } log "Run Complete"