###
### Functions to run with FIM Portal Workflow scripts
###
## Search for an object using ObjectType,DisplayName and return it’s ResourceID.
## When using directly after creation use -Wait $true to allow rechecking.
function ObjectExists
{
PARAM($ObjectType, $DisplayName, $Wait=$false)
END
{
$filter = “/$ObjectType[DisplayName='” + $DisplayName + “‘]”
if ($Wait) {$count = 1} else {$count = 10}
do
{
$fimObj = ExportFromFIM -Filter $filter
if ($fimObj -eq $null){start-sleep -s $count;$count += 1}
else {$count = 11}
}
while ($count -le 10)
if ($fimObj -eq $null) {Return 0}
elseif ($fimObj.Count) {Return 0}
else {Return ($fimObj.ResourceManagementObject.ObjectIdentifier).Replace(“urn:uuid:”,””)}
}
}
## Writes an event to the FIMCustomWF log (which must exist).
## Valid values for $Level are Error, Warning, Information, SuccessAudit, and FailureAudit. The default value is Information.
function WriteLog
{
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]
[string]$Message,
[string]$Level = “Information”,
[boolean]$Fatal = $false
)
END
{
if ($Verbose -or (-not $Verbose -and $Level -ne “Information”))
{
Write-EventLog -LogName Application -Source PowerShellActivity -Message ($ScriptName + “`n” + $Message) -EntryType $Level -EventID 0
if ($Fatal) {Throw $Message}
}
}
}
Function ImportToFIM
{
PARAM($ImportObject)
END
{
$Error.clear()
“Importing change” + ($ImportObject | Out-String -Width 100) + ($ImportObject.Changes | Out-String -Width 100) | WriteLog
try {Import-FIMConfig $ImportObject -ErrorAction SilentlyContinue}
catch {}
if ($Error.Count -gt 0)
{
if ($Error[0].Exception -ilike ‘*pending authorization*’) {} #Ignore
else {$Error[0].Exception | WriteLog -Level “Error” -Fatal $true}
}
}
}
Function ExportFromFIM
{
PARAM($Filter)
END
{
$Error.clear()
“Exporting FIM objects using filter $Filter” | WriteLog
try {$FIMObjects = Export-FIMConfig -CustomConfig $Filter -OnlyBaseResources -ErrorAction SilentlyContinue}
catch {$Error[0].Exception | WriteLog -Level “Error” -Fatal $true}
Return $FIMObjects
}
}
# Returns the requested value for a specifc Attribute in a Request object
Function RequestedValue
{
PARAM($ReqObj, $Attribute, $Mode)
END
{
$Changes = ($ReqObj.ResourceManagementObject.ResourceManagementAttributes | where {$_.AttributeName -eq ‘RequestParameter’}).Values
[string]$ReturnVal = “”
foreach ($XmlString in $Changes)
{
[xml]$change = $XmlString
if ($change.RequestParameter.PropertyName -eq $Attribute -and (-not $Mode -or $change.RequestParameter.Mode -eq $Mode))
{
[string]$value = “”
$value = $change.RequestParameter.Value.”#text”
if ($value -ne “”)
{
if ($ReturnVal -eq “”){$ReturnVal = $value}
else {$ReturnVal = $ReturnVal + “;” + $value}
}
}
}
Return $ReturnVal
}
}
# LocalDate: Returns the local date from a FIM UTC date. For use in email templates.
function LocalDate
{
PARAM($DateString)
END
{
$DT = Get-Date $DateString
$LocalDate = $DT.ToLocalTime()
$ReturnDate = Get-Date $LocalDate -Format “D”
$ReturnDate
}
}
###
### Run for all Workflow scripts
###
$ErrorActionPreference = “Continue”
$AdminGUID = “7fb2b853-24f0-4498-9534-4e10589723c4”
$AdminUrnGUID = “urn:uuid:7fb2b853-24f0-4498-9534-4e10589723c4”
if(@(get-pssnapin | where-object {$_.Name -eq “FIMAutomation”} ).count -eq 0) {add-pssnapin FIMAutomation}
$ProgressPreference=”SilentlyContinue”
## Get the Request Details
if ($fimwf)
{
“Request details: ” +
($fimwf | Out-String -Width 100) + “Workflow Data:`n” +
($fimwf.WorkflowDictionary | Out-String -Width 100 ) | WriteLog
}