Script Repository


Export Approval Requests to CSV

April 27, 2016
1228

The script exports Approval Request information to a CSV file once an Approval Request is approved, denied or cancelled. To use it with Adaxes, create a Business Rule triggered after updating an Approval Request that runs the script.

Parameter:

  • $csvFilePath - specifies the path to the CSV file that will store information on Approval Requests.

Edit Remove
PowerShell
$csvFilePath = "\\Server\share\ApprovalRequests.csv" # TODO: modify me

# Build a new row for the CSV file
switch ($Context.TargetObject.ApprovalState)
{
    "ADM_APPROVALSTATE_PENDING"
    {
        return
    }
    "ADM_APPROVALSTATE_APPROVED"
    {
        $state = "Approved"
    }
    "ADM_APPROVALSTATE_DENIED"
    {
        $state = "Denied"
    }
    "ADM_APPROVALSTATE_CANCELED"
    {
        $state = "Canceled"
    }
}

$reportEntry = New-Object PSObject
$reportEntry | Add-Member -Name "State" -Value $state -MemberType NoteProperty
$reportEntry | Add-Member -Name "Requestor" -Value $Context.TargetObject.Requestor.Get("name") -MemberType NoteProperty
$reportEntry | Add-Member -Name "Processed By" -Value $Context.TargetObject.ProcessedBy.Get("name") -MemberType NoteProperty
$reportEntry | Add-Member -Name "Creation Date" -Value $Context.TargetObject.CreationDate -MemberType NoteProperty
$reportEntry | Add-Member -Name "Target Object" -Value $Context.TargetObject.TargetObject.Get("name") -MemberType NoteProperty

if ($state -ne "Approved")
{
    $denialOrCancelingReason = $Context.TargetObject.DenialOrCancelingReason
}
else
{
    $denialOrCancelingReason = ""
}

$reportEntry | Add-Member -Name "Denial or Canceling Reason" -Value $denialOrCancelingReason -MemberType NoteProperty
$reportEntry | Add-Member -Name "Operation Description" -Value $Context.TargetObject.DescriptionOfOperationToApprove -MemberType NoteProperty

# Import the CSV file
try
{
    [System.Object[]]$rows = Import-Csv $csvFilePath
}
catch
{
    $rows = $NULL
}

# Add a new row to the CSV file
if ($rows)
{
    $rows += $reportEntry
}
else
{
    $rows = $reportEntry
}

# Save changes
$rows | Export-Csv -Path $csvFilePath -NoTypeInformation

Comments ( 0 )
No results found.
Leave a comment