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}
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 }
Thanks for the quick reply and explanation. After using the above suggestion, I get an identity parameter error:
Get-ADComputer : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty
What does your file look like? The message is saying it's not getting a computer name from your file.
See the screen shots from this article relative to what I responded with.
The file is just a straight .txt file with one computer name on each line. Mind sending over that link again? I don't think it made it in your last message.
https://www.oxfordsbsguy.com/2014/04/28/powershell-get-adcomputer-to-retrieve-computer-last-logon-date-part-1/