Skip to content

Instantly share code, notes, and snippets.

@SuryenduB
Last active February 3, 2021 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuryenduB/95d1152ccb74b9ca03b1f2f1d5d71b00 to your computer and use it in GitHub Desktop.
Save SuryenduB/95d1152ccb74b9ca03b1f2f1d5d71b00 to your computer and use it in GitHub Desktop.
Saviynt Management Agent for MIM (Powershell MA)
param (
[System.Collections.ObjectModel.KeyedCollection[[string], [Microsoft.MetadirectoryServices.ConfigParameter]]] $ConfigParameters,
[PSCredential] $PSCredential,
[Microsoft.MetadirectoryServices.OpenImportConnectionRunStep] $OpenImportConnectionRunStep,
[Microsoft.MetadirectoryServices.CloseImportConnectionRunStep] $CloseImportConnectionRunStep
)
[string]$watermark = $CloseImportConnectionRunStep.CustomData
Write-EventLog -LogName Application -Source "FIMSynchronizationService" -EventId 6801 -Category 1 -Message ("Ending " + $OpenImportConnectionRunStep.ImportType +" import saving new watermark: " + $watermark) -EntryType Information
(New-Object Microsoft.MetadirectoryServices.CloseImportConnectionResults($watermark)) #Return
param (
[System.Collections.ObjectModel.KeyedCollection[[string], [Microsoft.MetadirectoryServices.ConfigParameter]]] $ConfigParameters,
[PSCredential] $PSCredential,
[Microsoft.MetadirectoryServices.ImportRunStep] $GetImportEntriesRunStep,
[Microsoft.MetadirectoryServices.OpenImportConnectionRunStep] $OpenImportConnectionRunStep,
[Microsoft.MetadirectoryServices.Schema] [ValidateNotNull()] $Schema
)
#$customData = $($GetImportEntriesRunStep).CustomData
Import-Module (Join-Path -Path ([Microsoft.MetadirectoryServices.MAUtils]::MAFolder) -ChildPath "CommonModule.psm1") -Verbose:$false > $null
$importEntriesResults = New-Object -TypeName 'Microsoft.MetadirectoryServices.GetImportEntriesResults'
$importEntriesResults.CSEntries = New-Object -TypeName 'System.Collections.Generic.List[Microsoft.MetadirectoryServices.CSEntryChange]'
#region ConfigParameters
foreach ($ConfigParameter in $ConfigParameters)
{
$path = $ConfigParameters["Server"].Value
}
$username = $PSCredential.UserName.Split('\')[1] #Domain comes as part of PSCredential
$password = $PSCredential.GetNetworkCredential().password
$url = $PSCredential.UserName.Split('\')[0]
#endregion ConfigParameters
try
{
#region Get AuthHeader
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$body = "{`"username`":`"$username`",`"password`":`"$password`"}"
$AuthToken = Invoke-RestMethod "$url/ECM/api/login" -Method 'POST' -Headers $headers -Body $body
$apikey = $AuthToken.access_token
$headers.Add( "Authorization" ,"Bearer " + $apiKey)
$apiruntime = $(Get-Date).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss")
[string]$watermark = $GetImportEntriesRunStep.CustomData #Used in Delta Import
$importType = $OpenImportConnectionRunStep.ImportType
#endregion Get AuthHeader
#region Import Type
if($OpenImportConnectionRunStep.ImportType -eq 'Delta' )
{
Write-InfoToEventLog -infoMsg ("Offset = Delta Run Profile Sarted;") -category 1
$watermark = $GetImportEntriesRunStep.CustomData
Write-InfoToEventLog -infoMsg ("WATERMARK :: $watermark") -category 1
$body = "{
`n`"userQuery`":`"user.updatedate > '$watermark'` and user.updatedate <= '$apiruntime'`"
}"
}
else
{
$body = "{
`n`"advsearchcriteria`":{`"username`":`"*`"}
`n}"
}
#endregion
#region get totalCount
Write-InfoToEventLog -infoMsg ("BODY :: $body") -category 1
$responseTotal = Invoke-RestMethod "$url/ECM/$path/getUser" -Method 'POST' -Headers $headers -Body $body
$totalCounts=$responseTotal.totalcount
Write-InfoToEventLog -infoMsg ("$totalCounts") -category 1
#endregion
if ($totalCounts -gt 0)
{
#region Import Users V3
$response = @()
$displayCounts=5000
$count = [System.Math]::Ceiling($totalCounts/10000)*10000
Write-InfoToEventLog -infoMsg ("IMPORT USERS : $count") -category 1
for ($i = 0; $i -lt ($count+1); $i=$i+$displayCounts)
{
$response += $($(Invoke-RestMethod "$url/ECM/$path/getUser?offset=$i&max=$displayCounts" -Method 'POST' -Headers $headers -Body $body).userDetails)
Write-InfoToEventLog -infoMsg ("$i") -category 1
}
Write-InfoToEventLog -infoMsg ("Response Counts :: $($response.Count) ") -category 1
#endregion
}
}catch
{
$formatstring = "{0} : {1}`n{2}`n" +
" + CategoryInfo : {3}`n" +
" + FullyQualifiedErrorId : {4}`n"
$fields = $_.InvocationInfo.MyCommand.Name,
$_.ErrorDetails.Message,
$_.InvocationInfo.PositionMessage,
$_.CategoryInfo.ToString(),
$_.FullyQualifiedErrorId
$formatstring -f $fields | Out-File "C:\Program Files\Microsoft Forefront Identity Manager\2010\Synchronization Service\MaData\SAVIYNTMA.log" -Append
throw $_
}
#region Add CSEntries
foreach($user in $($response))
{
$csentry = [Microsoft.MetadirectoryServices.CSEntryChange]::Create()
$csentry.ObjectModificationType = "Add"
$csentry.ObjectType="user"
foreach ($attribute in $Schema.Types["user"].Attributes)
{
$value = $user.($attribute.Name)
if($value)
{
[void] $csentry.AttributeChanges.Add([Microsoft.MetadirectoryServices.AttributeChange]::CreateAttributeAdd($attribute.Name, $value))
}
}
$importEntriesResults.CSEntries.Add($csentry)
}
#endregion Add CSEntries
$importEntriesResults.CustomData = $(Get-Date).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss")
Write-InfoToEventLog -infoMsg ("WATERMARK :: $($importEntriesResults.CustomData)") -category 1
param (
[System.Collections.ObjectModel.KeyedCollection[[string], [Microsoft.MetadirectoryServices.ConfigParameter]]] $ConfigParameters,
[PSCredential] $PSCredential,
[Microsoft.MetadirectoryServices.OpenImportConnectionRunStep] $OpenImportConnectionRunStep,
[Microsoft.MetadirectoryServices.Schema] [ValidateNotNull()] $Schema
)
[string]$watermark = $OpenImportConnectionRunStep.CustomData #Used if Delta Import
#Write-EventLog -LogName Application -Source "FIMSynchronizationService" -EventId 6801 -Category 1 -Message ("Starting " + $OpenImportConnectionRunStep.ImportType +" import using watermark: " + $watermark) -EntryType Information
if($OpenImportConnectionRunStep.ImportType -eq 'Delta' -and !$watermark){throw ("Full Import Required. No watermark found.")}
Write-EventLog -LogName Application -Source "FIMSynchronizationService" -EventId 6801 -Category 1 -Message ("Starting " + $OpenImportConnectionRunStep.ImportType +" import using watermark: " + $watermark) -EntryType Information
(New-Object Microsoft.MetadirectoryServices.OpenImportConnectionResults($watermark)) #Return
Import-Module (Join-Path -Path ([Environment]::GetEnvironmentVariable("TEMP", [EnvironmentVariableTarget]::Machine)) -ChildPath "CommonModule.psm1") -Verbose:$false
$Schema = New-FIMSchema
$SchemaType = New-FIMSchemaType -Name "user" -LockAnchorAttributeDefinition
$SchemaType | Add-FIMSchemaAttribute -Name "employeeid" -DataType "String" -SupportedOperation ImportExport
$SchemaType | Add-FIMSchemaAttribute -Name "firstname" -DataType "String" -SupportedOperation ImportExport
$SchemaType | Add-FIMSchemaAttribute -Name "lastname" -DataType "String" -MultiValued -SupportedOperation ImportExport
$SchemaType | Add-FIMSchemaAttribute -Name "username" -Anchor -DataType "String" -SupportedOperation ImportExport
$SchemaType | Add-FIMSchemaAttribute -Name "statuskey" -DataType "String" -SupportedOperation ImportExport
$Schema.Types.Add($SchemaType)
$Schema | ConvertTo-Json | Out-File "C:\Windows\Temp\Schema.txt"
$Schema #Returnaa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment