Hello,
You can use the below script. To execute it, create a Scheduled Task configured for Domain-DNS object type and add a domain to the Activity Scope. The task will create a CSV file containing a column specifying whether a user with such a name is present in your environment. In the script:
- $sourceCSVFilePath – specifies the path to the source CSV file;
- $targetCSVFilePath – specifies the path to the resulting CSV file;
- $identityColumnName – specifies the name of the column in the source file that store names of users;
- $identityPropertyName – specifies the LDAP name of the property whose values will be compared to the values stored I the columns specified in the $identityColumnName variable;
- $statusColumnName – specifies the name of the column that will store user account statuses in the resulting CSV file.
$sourceCSVFilePath = "\\Server\Share\Users.csv" # TODO: modify me
$targetCSVFilePath = "\\Server\Share\Report.csv" # TODO: modify me
$identityColumnName = "Name" # TODO: modify me
$identityPropertyName = "name" # TODO: modify me
$statusColumnName = "Exists" # TODO: modify me
# Import CSV
$records = Import-Csv -Path $sourceCSVFilePath
# Specify settings for user search
$searcher = $Context.BindToObject("Adaxes://RootDSE")
$searcher.PageSize = 500
$searcher.VirtualRoot = $True
$usersFromCSV = @{}
$filter = New-Object "System.Text.StringBuilder"
foreach ($record in $records)
{
$userIdentity = $record.$identityColumnName
$record | Add-Member -MemberType NoteProperty -Name $statusColumnName -Value "False"
$usersFromCSV.Add($userIdentity, $record)
[void]$filter.Append("($identityPropertyName=$userIdentity)")
$remainder = 0
[void][System.Math]::DivRem($i, 500, [ref]$remainder)
if ((($i -ne 0) -and ($remainder -eq 0)) -or ($i -eq $importedUsers.Length - 1))
{
# Search users
$searcher.SearchFilter = "(&(sAMAccountType=805306368)(|" + $filter.ToString() + "))"
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
foreach ($searchResult in $searchResults)
{
$userIdentity = $searchResult.Properties[$identityPropertyName].Value
if ($usersFromCSV[$userIdentity] -eq $NULL)
{
continue
}
$usersFromCSV[$userIdentity].$statusColumnName = "True"
}
}
finally
{
# Release resources
if ($searchResultIterator) { $searchResultIterator.Dispose() }
}
# Clear filter
$filter.Length = 0
$filter.Capacity = 0
}
}
$usersFromCSV.Values | Export-Csv -Path $targetCSVFilePath -NoTypeInformation