Basic Powershell Scripting

Hello,

I'm trying to find the last logon date for a list of computers in a text file. Simple question for a simple script - why is it not pulling from the Get-Content, but rather pulling all computers? Thanks in advance.

$computers = Get-Content E:\Source\StaleInputList.txt

$date_with_offset= (Get-Date).AddDays(-120)

ForEach ($computer in $computers) {

Get-ADComputer -Filter {LastLogonDate -lt $date_with_offset } -Properties * | Sort LastLogonDate | FT Name, LastLogonDate -Autosize
}

Parents
  • You don't show what the file looks like.

    Get-Content will pull that whole file into memory and assign that to that variable you declared, this is by design and what you told your code to do.

    Secondly, Syntax error, you are not telling the Get-ADComputer cmdlet what Identity to use.

    You can use Get-Content or Import-Csv for this use case for list. If you file does not have headers, depending on what you try to do you'd need to add them on the fly.

    Try this...


        $date_with_offset = (Get-Date).AddDays(-120)

        ForEach ($computer in (Import-Csv -Path 'E:\Source\StaleInputList.txt'))
        {
            Get-ADComputer -Identity $computer -Filter {LastLogonDate -lt $date_with_offset } -Properties * |
            Sort LastLogonDate |
            FT Name, LastLogonDate -Autosize
        }

Reply Children