Author Posts

May 4, 2016 at 1:43 pm

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.

May 4, 2016 at 4:29 pm

$names = Get-Content \\path\to\textfile.txt

$results = foreach ($name in $names){
   $itemobj = Get-ADComputer -Filter {Name -eq $name} -Properties PasswordLastSet |
    Select-Object Name,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

May 5, 2016 at 3:25 pm

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.

May 5, 2016 at 5:52 pm

I modified my previous comment. Try it again.

May 6, 2016 at 12:39 pm

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 |
Select-Object Name, PasswordLastSet, LastLogonDate, CanonicalName
If ($itemobj)
{
[PSCustomObject]@{Name = $itemobj.Name; PasswordLastSet = $itemobj.PasswordLastSet; `
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"};
} # End Else
} # End Foreach
$results | Export-Csv $Logfile -NoTypeInformation
}

Main;
return;

Both output options need to match, otherwise if the first item in the input file doesn't exist, the extra columns won't be created or populated. Strange.