Script Repository


User properties

September 22, 2016
1798

The script can be used to create and send an HTML-formatted report containing properties of a user account. For example, it can be used to send properties of a newly created user account to the user's manager.

How to run a PowerShell script after creating a User

Parameters:

  • $to - specifies the email address of the notification recipient;
  • $subject - specifies the email message subject;
  • $reportHeader - specifies the email message header;
  • $reportFooter - specifies the email message footer;
  • $ignoredProperties - specifies the properties that won't be e-mailed.
Edit Remove
PowerShell
$to = "recipient@domain.com" # TODO: modify me
$subject = "All properties of new user %username%" # TODO: modify me
$reportHeader = "<h3><b>All properties of new user %username%</b></h3><br/><table border='0'>" # TODO: modify me
$reportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me
$ignoredProperties = @("objectClass") # TODO: modify me

# Get property list
$propertyList = $Context.Action.PropertyList

# Get display names for all properties
$culture = [System.Globalization.CultureInfo]::CurrentCulture
$attributeFriendlyNamesCache = [Softerra.Adaxes.Directory.AttributeFriendlyNamesCache]::GetInstance($culture)

# Build report
for ($i = 0; $i -lt $propertyList.PropertyCount; $i++)
{
    $propertyEntry = $propertyList.Item($i)
    $propertyLdapName = $propertyEntry.Name
    
    # Skip ignored properties
    if ($ignoredProperties -contains $propertyLdapName)
    {
        continue
    }

    if ($propertyEntry.Values -eq $NULL)
    {
        continue
    }

    # Add property name to the report
    if ($attributeFriendlyNamesCache.HasFriendlyName($propertyLdapName))
    {
        $propertyName = $attributeFriendlyNamesCache.GetFriendlyName($propertyLdapName, "user")
    }
    else
    {
        $propertyName = $propertyLdapName
    }
    
    $reportHeader += "<tr><td>$propertyName`:</td><td>"
    
    # Add property value to the report
    foreach ($propertyValue in $propertyEntry.Values)
    {
            # Convert value for specific properties
            switch ($propertyLdapName)
            {
                "accountExpires"
                {
                    $value = "%accountExpires%"
                }
                "unicodePwd"
                {
                    $value = "%unicodePwd%"
                }
                "pwdLastSet"
                {
                    $value = "%pwdLastSet%"
                }
                default
                {
                    $value = ($propertyValue.GetObjectProperty([ref]$propertyEntry.ADsType)).ToString()
                }
            }
            
            $reportHeader += "$value;"
    }
    $reportHeader += "</td></tr>"
}

$reportHeader += "</table>"
$htmlBody = $reportHeader + $reportFooter

# Send email
$Context.SendMail($to, $subject, $NULL, $htmlBody)


Comments ( 0 )
No results found.
Leave a comment