A script that updates the computer object every time a user logs on, or even every time a different person log onto a computer, will generate a lot of replication traffic. A better scheme might be a logon script that appends the date, time, user name, and
computer name to a shared log file (comma delimited). the resulting log file can be opened in Excel, where the lines can be sorted by user an date. The logon script can be as simple as the following batch file:
@echo off
echo %date% %time%,%UserName%,%ComputerName% >> \\MyServer\MyShare\LogUsers.log
If you want to use your scheme, the following snippet, showing how to query for a computer with a given value assigned to description, may help:
$Computers = Import-Csv -Path "Computers.csv"
ForEach ($Computer In $Computers)
{
$User = $Computer.UserName
$Computers = Get-ADComputer -Filter {Description -eq $User}
# Output to CSV.
}
$Computers will be a collection of objects, with properties such as Name, SamAccountName, and DNSHostName. Note that Name is the Relative Distinguished Name of the computer, which does not necessarily uniquely identify the object in AD.
One reason we hesitate to offer code is that we do not know how your attempts have failed. I have assumed you cannot query for the computers. I also assumed your CSV has a header line defining the field UserName. But you might have had trouble outputting
results to a CSV. It would help to provide the code you have so far, so we can see what is wrong.
Richard Mueller - MVP Enterprise Mobility (Identity and Access)