Need help with file output

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Don Reese Don Reese 4 months, 3 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #38599
    Profile photo of Don Reese
    Don Reese
    Participant

    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.

    #38610
    Profile photo of random commandline
    random commandline
    Participant
    $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
    
    #38666
    Profile photo of Don Reese
    Don Reese
    Participant

    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.

    #38668
    Profile photo of random commandline
    random commandline
    Participant

    I modified my previous comment. Try it again.

    #38700
    Profile photo of Don Reese
    Don Reese
    Participant

    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.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.