We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Export user properties to CSV file

December 13, 2023 Views: 1342

Script 1: Email properties of the target user

The script exports specified properties of the target user to a CSV file and sends the file via email. To execute the script, create a scheduled task, a custom command or a business rule configured for the User object type.

Parameters:

  • $csvFilePath - Specifies the path to the CSV file to be created.
  • $removeCsvFile - Specifies whether the CSV file will be removed after sending the email notification.
  • $propertiesToExport - Specifies the LDAP names of the properties whose values will be present in the CSV file.
  • $valueSeparator - Specifies a character that will be used to separate values of multi-valued properties in the CSV file.
  • $to - Specifies the recipient email address.
  • $subject - Specifies the email notification subject.
  • $from - Specifies the email address from which the email notification will be sent.
  • $mailServer - Specifies the FQDN of the mail server that will be used to deliver the email notification.
  • $body - Specifies the email notification body.
Edit Remove
PowerShell
$csvFilePath = "C:\Scripts\Report.csv" # TODO: modify me
$removeCsvFile = $True # TODO: modify me
$propertiesToExport = @("givenName", "middleName", "sn", "title", "co", "mail") # TODO: modify me
$valueSeparator = ";" # TODO: modify me

# Email message settings
$to = "recipient@domain.com" # TODO: modify me
$subject = "Properties of user %fullname%" # TODO: modify me
$from = "noreply@domain.com" # TODO: Modify me
$mailServer = "mail.domain.com" # TODO: Modify me
$body = "Properties of user %fullname%" # TODO: Modify me

$propertyNameToValue = @{}
foreach ($propertyName in $propertiesToExport)
{
    try
    {
        $values = $Context.TargetObject.GetEx($propertyName)
    }
    catch
    {
        $values = $NULL
    }
    
    $value = $values -join $valueSeparator
    $propertyNameToValue.Add($propertyName, $value)
}

$record = New-Object PSObject -Property $propertyNameToValue
@($record) | Export-Csv -Path $csvFilePath -NoTypeInformation

# Send mail
Send-MailMessage -to $to -From $from -Subject $subject -Body $body -SmtpServer $mailServer -Attachments $csvFilePath

if ($removeCsvFile)
{
    # Remove CSV File
    Remove-Item $csvFilePath -Force
}

Script 2: Export properties of all users

The script exports specified properties of all users to a CSV file. To execute the script, create a scheduled task configured for the Domain object type and add a managed domain to the Activity Scope.

Parameters:

  • $csvFilePath - Specifies the path to the CSV file to be created.
  • $propertiesToExport - Specifies the LDAP names of the properties whose values will be present in the CSV file.
  • $valueSeparator - Specifies a character that will be used to separate values of multi-valued properties in the CSV file.
Edit Remove
PowerShell
$csvFilePath = "C:\Scripts\Report.csv" # TODO: modify me
$propertiesToExport = @("givenName", "middleName", "sn", "title", "co", "mail", "userAccountControl") # TODO: modify me
$valueSeparator = ";" # TODO: modify me

function CreateCSVrecord ($searchResult, $propertyNames)
{
    $propertyNameToValue = [ordered]@{}
    $user = $Context.BindToObjectBySearchResult($searchResult)
    
    foreach ($propertyName in $propertyNames)
    {
        try
        {
            $values = $user.GetEx($propertyName)
        }
        catch
        {
            $values = $NULL
        }
        
        if ($propertyName -eq "userAccountControl")
        {
            if ($values[0] -band 2)
            {
                $values = "Disabled"
            }
            else
            {
                $values = "Enabled"
            }
        }
        
        $value = $values -join $valueSeparator
        $propertyNameToValue.Add($propertyName, $value)
    }
    
    return New-Object PSObject -Property $propertyNameToValue
}

# Search parameters
$searcher = $Context.TargetObject
$searcher.Criteria = New-AdmCriteria "user"
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SetPropertiesToLoad($propertiesToExport)
$searcher.VirtualRoot = $True

try
{
    # Execute search
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    $records = New-Object System.Collections.ArrayList
    
    foreach ($searchResult in $searchResults)
    {
        $record = CreateCSVrecord $searchResult $propertiesToExport
        $records.Add($record)
    }
}
finally
{
    # Release resources
    if ($searchResultIterator){ $searchResultIterator.Dispose() }
}

# Export CSV
$records | Export-Csv -Path $csvFilePath -NoTypeInformation
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers