Teched Demo 3: SelectUnique.ps1

PARAM([string]$AttributeName,[string]$ObjectType,[boolean]$Verbose=$true)
###
### Select the first unique value from options passed in WorkflowData.
### Return the selected value as [\\WorkflowData\Result]
###
### Called using FIMPowershell Workflow Activity.
###     Parameters:
###       -AttributeName  The Attribute to be checked for uniqueness
###       -ObjectType     The object type to check uniqueness against
###       -Verbose        If $true writes verbose logs to the FIMCustomWF event log
###
###     WorkflowData: 
###       Inputs:  One or more WorkflowData values named “OptionN” where N is a number showing priority
###       Returns: The selected value in [//WorkflowData/Result]
###

### Get FIMPowershell.ps1 from http://technet.microsoft.com/en-us/library/ff720152(v=ws.10).aspx
. C:\scripts\FIMPowerShell.ps1

### Library of shared functions for Workflow scripts
. C:\scripts\WFFunctions.ps1
###
### MAIN
###

### Check we got everything we need
if (-not $AttributeName) {“AtributeName parameter is required” | WriteLog -Level “Error” -Fatal $true}
if (-not $ObjectType) {“ObjectType parameter is required” | WriteLog -Level “Error” -Fatal $true}
if (-not $fimwf) {“Failed to get workflow details from the FIM Request” | WriteLog -Level “Error” -Fatal $true}
if ($Verbose) {“Request details: ” + ($fimwf  | Out-String -Width 100) + “Workflow Data:`n” + ($fimwf.WorkflowDictionary  | Out-String -Width 100 ) | WriteLog }

### Loop through the WFData parameters, testing for each OptionN value
$conflict = $true
foreach ($wfparam in $fimwf.WorkflowDictionary.Keys | sort)
{
    if ($conflict -and $wfparam.StartsWith(“Option”))
    {
        $filter = “/{0}[{1}='{2}’ and not(ObjectID='{3}’)]” -f $ObjectType,$AttributeName,$fimwf.WorkflowDictionary.($wfparam),$fimwf.TargetId.Guid
        if ($Verbose) {$wfparam + “: Checking for conflicts with filter ” + $filter | WriteLog}
        $obj = Export-FimConfig -CustomConfig $filter -OnlyBaseResources
        if (-not $obj)
        {
            $conflict = $false
            $ReturnValue = $fimwf.WorkflowDictionary.($wfparam)
        }
        else {if ($Verbose) {$wfparam + “: Conflict found” | WriteLog} }
    }
}

if (-not $conflict -and $ReturnValue)
{
    if ($Verbose) {“Returning value ” +  $ReturnValue | WriteLog}
    $fimwf.WorkflowDictionary.Add(“Result”,$ReturnValue)
}
else
{
    “Failed to find a unique value for $AttributeName” | WriteLog -Level “Error” -Fatal $true
}

Leave a Reply

Your email address will not be published. Required fields are marked *


*