Hello Robert,
Thank you for the confirmation. Please, find the updated script below. In the script, we added the $csvFilePath variable that specified the path to the CSV file that will be created.
$win32UserFilter = "NOT SID = 'S-1-5-18' AND NOT SID = 'S-1-5-19' AND NOT SID = 'S-1-5-20'" # Exclude well-known SIDs, such as NETWORK SERVICE
# CSV File settings
$csvFilePath = '\\Server\Share\Report.csv' # TODO: modify me
function SearchObjects ($filter, $path)
{
$searcher = $Context.BindToObject($path)
$searcher.SearchFilter = $filter
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
return ,$searchResults
}
finally
{
# Release resources
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}
}
# Search results
$searchResults = SearchObjects "(&(objectCategory=computer)(dNSHostName=*))" $Context.TargetObject.AdsPath
$domainName = $Context.GetObjectDomain("%distinguishedName%")
$userSidsToUserName = @{}
$records = New-Object System.Collections.ArrayList
foreach ($searchResult in $searchResults)
{
$dNSHostName = $searchResult.Properties["dNSHostName"].Value
if (!(Test-Connection -ComputerName $dNSHostName -Quiet -Count 1))
{
$Context.LogMessage("Cannot connect to computer $dNSHostName", "Warning")
continue
}
# Get the last logged on user
try
{
$lastUser = Get-WmiObject -Class Win32_UserProfile -ComputerName $dNSHostName -Filter $win32UserFilter -ErrorAction Stop | Sort-Object -Property @{Expression = {$_.ConvertToDateTime($_.LastUseTime)}; Descending = $True} | Select-Object -First 1
}
catch
{
$Context.LogMessage("An error occurs when getting user information from computer $dNSHostName. Error: " + $_.Exception.Message, "Warning")
continue
}
# Build filter to find the user
$userSID = $lastUser.SID
if (!$userSidsToUserName.Contains($userSID))
{
$filter = [Softerra.Adaxes.Ldap.FilterBuilder]::Create("ObjectSid", $userSID)
$userSearchResults = SearchObjects $filter "Adaxes://$domainName/rootDSE"
if ($userSearchResults.Length -eq 0)
{
$Context.LogMessage("Cannot find user with SID '$userSID' for computer $dNSHostName. Probably, it is a local account.", "Warning")
continue
}
$userSidsToUserName.Add($userSID, $userSearchResults[0].Properties["sAMAccountName"].Value)
}
# Get Username
$username = $userSidsToUserName[$userSID]
# Get parent name
$computerDN = New-Object Softerra.Adaxes.Ldap.DN $searchResults[0].Properties["distinguishedName"].Value
$parentName = $computerDN.Parent.Leaf.Value
# Get LastUseTime
$lastUseTime = $lastUser.ConvertToDateTime($lastUser.LastUseTime)
# Log
$Context.LogMessage("$parentName - $dNSHostName - $username - $lastUseTime", "Information")
$recordProperties = [ordered]@{
'Parent name' = $parentName
'dNSHostName' = $dNSHostName
'User name' = $username
'lastUseTime' = $lastUseTime
}
$record = New-Object PSObject -Property $recordProperties
[void]$records.Add($record)
}
# Export to CSV
$records | Export-Csv -Path $csvFilePath -NoTypeInformation