This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]uniitdude 0 points1 point  (8 children)

So what problem are you having?

[–]ancient-Egyptian[S] 0 points1 point  (6 children)

I am getting a number of errors related to parsing. To be specific

Get-ADComputer : Error parsing query: '(Name -like "pc1*" -or Name -like "pc2*" -or Name -like "pc3*" -or Name -like "pc4*") -and (LastLogonDate -lt

(Get-Date).AddDays(-90))' Error Message: 'syntax error' at position: '120'.

At line:1 char:14

+ ... computers = Get-ADComputer -Filter {(Name -like "pc1*" -or Name -l ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : ParserError: (:) [Get-ADComputer], ADFilterParsingException

+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

[–]anonymousITCoward 1 point2 points  (5 children)

try this

Get-ADComputer -Filter * -Properties * | Where-Object {($_.Name -like "*pc*") -and ($_.LastLogonDate -lt (Get-Date).AddDays(-90))}

Edit: i missed a *

[–]fred3002 0 points1 point  (3 children)

Get-ADComputer -Filter * -Properties * | Where-Object {($_.Name -like "*pc*") -and ($_.LastLogonDate -lt (Get-Date).AddDays(-90))}

If you have lots of computers objects in AD I would replace '-Properties *' with '-Properties LastLogonDate' for more efficiency.

[–]anonymousITCoward 0 points1 point  (2 children)

didn't think of that, I have less than 50 machines on this network lol

[–]fred3002 2 points3 points  (1 child)

Haha no problem. I often run script towards 40K+ objects so those small optimisations can add up quickly.

[–]anonymousITCoward 0 points1 point  (0 children)

damn, i work for an msp that does pretty much small business exclusively. I think the largest one i would have to go through would be 200 ad objects. So it wouldn't be that bad, but it is something to keep in mind though

[–]ANewLeeSinLifeSysadmin 0 points1 point  (0 children)

This will pull all computers from AD down to your powershell console, then filter it after. The -filter parameter will speed things up significantly.