# Need help with file output

n00b question, I know, but I’ve tried three ways from Sunday to solve it on my own.
I’m reading in a text file of computer names, checking against AD to see if it exists and retrieving the PasswordLastSet and trying to write the Name and this value out to a textfile.
I have a function that adds the content:
function WriteLog($message) { Add-Content$Logfile -value $message -passthru; return; } and here is my problem line: Get-ADComputer -Filter {Name -like “$item”} -Properties Name, PasswordLastSet | WriteLog Name, PasswordLastSet;
which is embedded in a foreach loop reading the input file.

I just want Name, PasswordLastSet values in the output file.

Help?!? Thx.

$names = Get-Content \\path\to\textfile.txt$results = foreach ($name in$names){
$itemobj = Get-ADComputer -Filter {Name -eq$name} -Properties PasswordLastSet |
If ($itemobj){[PSCustomObject]@{ Name =$itemobj.Name
PasswordLastSet = $itemobj.PasswordLastSet }} Else { [PSCustomObject]@{ Name =$name
PasswordLastSet = "$name not in AD" }} } # End Foreach$results | export-csv \\path\to\results.csv -NoTypeInformation


We’re getting closer, but not quite there. I’ve tried dozens of different iterations on this. The Out-file gives me the desired data, but not in the desired format. I would like to get a nice, clean csv file with the dnshostname and either the passwordlastset or a string saying “not found in AD”, or something similar. Below is my full script at the moment:

[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]$filename = $(throw “-Filename is required.”) ) function WriteLog($message)
{
Add-Content $Logfile -value$message;
return;
}

function Main()
{

$items = Get-Content -Path$filename

$results = foreach ($item in $items) { try { Get-ADComputer -Identity$item -Properties PasswordLastSet | Select-Object DNSHostName, PasswordLastSet;
}
catch
{
WriteLog "$item, 'No Computer Object in AD'"; } }$results | Out-File $Logfile  }$d = [DateTime]::Today.AddDays(-365);
$Logfile = “.\PWLastSet.csv” Main; return; I would appreciate any assistance and suggestions. I modified my previous comment. Try it again. Thank you very much for the great assistance! Here is my final (at-the-moment) script: [CmdletBinding()] Param( [Parameter(Mandatory=$True,Position=1)]
$filename =$(throw “-Filename is required.”),
[Parameter(Mandatory=$True,Position=2)]$logfile = $(throw “-LogFile is required.”) ) function Main() {$items = Get-Content -Path $filename $results = Foreach ($item in$items)
{
$itemobj = Get-ADComputer -Filter {Name -eq$item} -Properties PasswordLastSet, LastLogonDate, CanonicalName |
If ($itemobj) { [PSCustomObject]@{Name =$itemobj.Name; PasswordLastSet = $itemobj.PasswordLastSet; &#8175; LastLogonDate =$itemobj.LastLogonDate; CanonicalName = $itemobj.CanonicalName}; } #End If Else { [PSCustomObject]@{Name =$item;	PasswordLastSet = "Computer object not found in AD"; LastLogonDate = "N/A"; CanonicalName = "N/A"};
$results | Export-Csv$Logfile -NoTypeInformation