Creating scripts for custom column generation

This article describes how to generate values of report custom columns using PowerShell scripts.

To assign a column value, you need to use a variable called $Context. It is a predefined PowerShell variable of the ReportCustomColumnScriptContextClass type. Use the Value property of the variable to set the column value.

$Context.Value = "My value"

Getting information about report items

To get information about the object for which a column value is generated, use the $Context.GetDirectoryObject method. The object returned by the method supports the IADs interface.

The following code sample sets the custom column value to the UPN suffix of a user.

# Get object
$user = $Context.GetDirectoryObject()

# Get property value
$upn = $user.Get("userPrincipalName")

# Set column value
$upnSuffix = $upn.Substring($upn.LastIndexOf("@") + 1)
$Context.Value = $upnSuffix

Alternatively, you can access information on the report item using the $Context.ReportItem property. It supports the IAdmListItem interface. If the object represented by the item is a log record or a custom object, the property also supports the IAdmListItemLogRecord or IAdmListItemCustom interface respectively.

$Context.Value = $Context.ReportItem.LogRecord.StartTime

Getting information about report recipient

To get information about the user for which the report is generated, use the $Context.Initiator property.

if (-not($Context.Initiator.Username.Contains("admin")))
{
    $Context.Value = "Not allowed to view"
}

Alternatively, you can use value references (e.g. %department% or %title%).

$recipientInfo = "%fullname%, %title% at %department%"

Improving performance

The script is executed for batches of objects rather than one at a time. For better performance, you can share information among iterations of the script within a batch. In the following example, a value requested from a web service is shared.

if ($myvalue -eq $null) # check whether value has already been requested
{
    # Get value from web service 
    $service = New-WebServiceProxy `
        -uri "http://www.company.com/Service.asmx?WSDL"
    $fileServer = $service.RequestSomething("My Argument")
}

# Get user
$user = $Context.GetDirectoryObject()

# Get username
$username = $user.Get("userPrincipalName")
$username, $null = $username.Split("@")

# Set column value
$Context.Value = "Personal Share: \\$fileServer\Shares\$username"

Also, you can optimize performance by setting column values in the script used for report generation. For details, see Setting values for custom columns.

See also