Script repository
The scripts export user properties to a CSV file.
Script 1: Email properties of the target user
The script exports the specified properties of the target user to a CSV file and sends the file via email. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.
Parameters
$csvFilePath- the path to the CSV file to be created.$removeCsvFile- whether the CSV file will be removed after sending the email notification.$propertiesToExport- the names of the properties whose values will be present in the CSV file.$valueSeparator- a character that will be used to separate values of multi-valued properties in the CSV file.$to- the address of the email notification recipient.$subject- the email notification subject.$from- the address from which the email notification will be sent.$mailServer- the FQDN of the mail server that will be used to deliver the email notification.$body- the email notification body.
$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 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 the 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 of the task. The domain will only be used to trigger execution of the scheduled task.
Parameters
$csvFilePath- the path to the CSV file to be created.$propertiesToExport- the names of the properties whose values will be present in the CSV file.$valueSeparator- a character that will be used to separate values of multi-valued properties in the CSV file.
$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 7
You must be signed in to comment.
DRiVSSi
Hello,
We want to use the second script to export the users of a certain OU to a csv file. With a scheduled task we have set the task to two OUs, but all other OUs will be exported as well.
And we also want to export Adaxes fields such as "adm-CustomAttributeText4" and "adm-CustomAttributeText5" in addition to AD fields. What should the code look like?
Thank you very much
Support
Hello,
> With a scheduled task we have set the task to two OUs, but all other OUs will be exported as well.
For the approach to work, you need to create a scheduled task configured for the Organizational Unit object type and add the required OUs to the Activity Scope as This object only.
There will be a separate file created for each OU, so it is recommended to make the CSV file name dynamic using value references. For example:
Also, for the script to search only under the target OU, replace this line
with the below one
>And we also want to export Adaxes fields such as "adm-CustomAttributeText4" and "adm-CustomAttributeText5" in addition to AD fields. What should the code look like?
As it is mentioned in the script description, you need to specify the names of all the required properties in the $propertiesToExport variable. For example:
Lastly, it might be much easier to use a report and schedule it instead of using a scheduled task. In this case, the following tutorials will be helpful:
Create report
Schedule reports
DRiVSSi
Thank you very much, with the adjustments only the selected OUs are now exported.
However, with the fields which should be exported, we also want to export the fields %adm-CustomAttributeText5% and %adm-CustomAttributeText4% of the user, which are only in Adaxes and not in the AD. How can Adaxes custom fields be added?
We have tried with:
but this does not work.
Support
Hello,
In the $propertiesToExport variable, you specified value references to the custom attributes instead of their names. To achieve the required, remove % characters around the custom attribute names. The variable declaration should look like this:
Mike
How can I exclude disabled user accounts from the "Export properties of all users" script?
Support
Hello Mike,
We updated the script so it exports only enabled accounts. Please, find it below.
Mike
Thank you! This worked perfectly!