Public/new-AllegisIDNIdentityProfileAttributeTransform.ps1

function new-AllegisIDNIdentityProfileAttributeTransform 
{
param( 
     [cmdletbinding(
        DefaultParameterSetName='AccountAttribute'
    )]
    [string]$orgname,
    [Parameter(Position = 0,Mandatory=$true)]$identityAttributeName,
    [Parameter(ParameterSetName='AccountAttribute',Mandatory=$false)]$sourcename,
    [Parameter(ParameterSetName='AccountAttribute',Mandatory=$false)]$sourceattribute,
    [string]$accesstoken,
    [string]$rule,
    [string]$transform,
    [switch]$PurgeAndRefreshReferenceInfo
)
    if ($PurgeAndRefreshReferenceInfo -eq $true -or $Global:idn_identityAttributes -eq $null -or $Global:idn_rule -eq $null -or $Global:idn_transformnames -eq $null -or $Global:idn_sourcesWithImportSchema -eq $null -or $Global:idn_orgname -ne $orgname)
    {#populate data
        Set-Variable -Scope 'Global' -Name 'idn_orgname' -Value $orgname
        Set-Variable -Scope 'Global' -Name 'idn_rule' -Value (get-AllegisIDNrule -orgName $sourceOrg -accessToken $accesstoken)
        Set-Variable -Scope 'Global' -Name 'idn_transformnames' -Value ((get-AllegisIDNtransform -orgName $sourceOrg -accessToken $accesstoken).items.id)
        Set-Variable -Scope 'Global' -Name 'idn_sourcesWithImportSchema' -Value (get-AllegisIDNsource -orgName $sourceOrg -accessToken $accesstoken -importschema)
        Set-Variable -Scope 'Global' -Name 'idn_identityAttributes' -Value (get-AllegisIDNIdentityAttribute -orgName $sourceOrg -accessToken $accesstoken)
    }
    $rules=$Global:idn_rule
    $transformnames=$Global:idn_transformnames
    $sourcesWithImportSchema=$Global:idn_sourcesWithImportSchema
    $identityAttributes=$Global:idn_identityAttributes
    if ($identityAttributeName -cnotin $identityattributes.name){write-warning "identity attribute name does not exactly match an existing identity attribute";return}
    if ($rule.length -gt 0){
        $type='rule'
    }elseif($transform.Length -gt 0){
        $type='reference'
    }else{
        $type='accountAttribute'
    }
    
    
    $source=$sourcesWithImportSchema.where{$_.name -eq $sourcename}[0]
    if ($source.importschema.attributes.where{$_.name -eq $sourceattribute} -eq $null){write-warning -Message "unable to find $sourcename : $sourceattribute in `sourcesWithImportSchema";return}
    if (($transform.length -gt 0 -and $transform -notin $transformnames)){write-warning -Message "provided transform name did not appear to be valid $transform";return}
    if ($accountattribute.length -eq 0){
        $accountattribute=$sourcesWithImportSchema.where{$_.name -eq $sourcename}.importschema.identityAttribute[0]
    }else{
        $accountattribute=$sourcesWithImportSchema.where{$_.name -eq $sourcename}.importschema.attributes.name.where{$_ -eq $sourceattribute}[0]
    }
    switch ($type){
        rule{
            $validrules=$rules.items.where{$_.type -eq 'IdentityAttribute'}
            $ruleObj=$validrules.where{$_.name -eq $rule}[0]
            $accountattribute=[pscustomobject]@{
                id=$ruleObj.id
                name=$ruleObj.name
            }
            $attributeTransform=[pscustomobject]@{
                attributename=$identityattributename
                attributes=$accountattribute
                type=$type
            }
        }
        reference{
            $accountattribute=[pscustomobject]@{
                applicationId=$source.externalid
                applicationName="$($source.health.name)"
                attributeName=$accountattribute
                sourceName=$sourcename
            }
            $input=[pscustomobject]@{
                attributes=$accountattribute
                type='accountAttribute'
            }
            $attributes=[pscustomobject]@{
                id=$transform
                input=$input
            }
            $attributeTransform=[pscustomobject]@{
                attributename=$identityattributename
                attributes=$attributes
                type=$type
            }
        }
        accountAttribute{
            $accountattribute=[pscustomobject]@{
                applicationId=$source.externalid
                applicationName="$($source.health.name)"
                attributeName=$accountattribute
                sourceName=$sourcename
            }
            $attributeTransform=[pscustomobject]@{
                attributename=$identityattributename
                attributes=$accountattribute
                type=$type
            }
        }
    }
    return $attributeTransform
}