0

I need to filter out 4 machines prior to exporting to a csv file. I have no clue how to filter them out. I tried the IF clause but this produced nothing. Please help.

$old = (Get-Date).AddDays(-90) # Modify the -90 to match your threshold $oldComputers = Get-ADComputer -SearchBase "OU=Workstations,DC=Corporate,DC=Local" -SearchScope 2 -Filter { PasswordLastSet -le $old } -Properties * $oldComputers if ($_.name -notlike "1919DD" -or $_.name -notlike "1919SMAHESHWARE" -or $_.name -notlike "1919IETEST" -or $_.name -notlike "1920BPASCERITB") { Export-Csv c:\temp\Over90DaysMachines.csv -NoTypeInformation -Force -Append } 

3 Answers 3

1

For one thing, to be able to use the current object variable ($_) you need a pipeline context. Simply putting an if statement after echoing a variable doesn't automagically feed the echoed value(s) into the if statement. You need to change this:

$oldComputers if ($_.Name -notlike "1919DD" -or ...) { Export-Csv c:\temp\Over90DaysMachines.csv -NoTypeInformation -Force -Append } 

into something like this:

$oldComputers | Where-Object { $_.Name -notlike "1919DD" -or ... } | Export-Csv c:\temp\Over90DaysMachines.csv -NoType -Force 

However, even with that change your filter won't work correctly, because you connected the -notlike clauses via -or when you should have used -and. You obviously meant to process objects only if their name doesn't match any of the given values. But for your logical expression to evaluate to $false the name would have to match all of the reference value at the same time. Which clearly isn't possible, thus your expression always evaluates to $true.

Example:

Assume that you have a variable $v that should not be equal to either A, B, or C. Applying your logic, the expression would look somewhat like this in PowerShell:

($v -notlike 'A') -or ($v -notlike 'B') -or ($v -notlike 'C') 

If $v takes for instance the value A that expression becomes

 ('A' -notlike 'A') -or ('A' -notlike 'B') -or ('A' -notlike 'C') ⇔ ($false) -or ($true) -or ($true) ⇔ $true

To check if a give value equals neither of the reference values you need to connect the clauses via -and:

 ('A' -notlike 'A') -and ('A' -notlike 'B') -and ('A' -notlike 'C') ⇔ ($false) -and ($true) -and ($true) ⇔ $false
$oldComputers | Where-Object { $_.Name -notlike "1919DD" -and $_.Name -notlike "1919SMAHESHWARE" -and $_.Name -notlike "1919IETEST" -and $_.Name -notlike "1920BPASCERITB" } | Export-Csv c:\temp\Over90DaysMachines.csv -NoType -Force 

Note BTW, that the -notlike operator behaves exactly like the -ne operator when the reference string doesn't contain wildcard characters. If you're not doing fuzzy matches anyway you could simplify your expression by checking if the given name is (not) found in an array of names instead of doing multiple checks for (in)equality:

$excludes = '1919DD', '1919SMAHESHWARE', '1919IETEST', '1920BPASCERITB' $oldComputers | Where-Object { $excludes -notcontains $_.Name } | Export-Csv c:\temp\Over90DaysMachines.csv -NoType -Force 

Another option would be a regular expression (non-)match:

$oldComputers | Where-Object { $_.Name -notmatch '^1919DD|1919SMAHESHWARE|1919IETEST|1920BPASCERITB$' } | Export-Csv c:\temp\Over90DaysMachines.csv -NoType -Force 
Sign up to request clarification or add additional context in comments.

1 Comment

as usual, you not only provide the solution, but school me every time so it actually makes perfect sense.. thanks bud.
1

I'm guessing the code in the OP is a fragment from a larger script. Presumably it is the body or part of the body of a ForEach-Object. (If not then $_ doesn't make sense in this context). However a ForEach-Object isn't necessary. You can filter out the unwanted computers as follows:

$old = (Get-Date).AddDays(-90) # Modify the -90 to match your threshold $oldComputers = Get-ADComputer -SearchBase "OU=Workstations,DC=Corporate,DC=Local" -SearchScope 2 -Filter { PasswordLastSet -le $old } -Properties * $oldComputers | Where-Object { $_.name -notin "1919SMAHESHWARE","1919IETEST", "1920BPASCERITB" } | Export-Csv c:\temp\Over90DaysMachines.csv -NoTypeInformation -Force -Append 

This assumes that $oldComputers is an array of object where each object has a property name and the value of name is a string like "server1", "server2", etc. The script in the OP outputs $oldComputers so verify it looks like a set of objects, with a name property consisting of a string where the servers to be excluded are spelled exactly as listed in the OP.

2 Comments

It should be noted that -notin requires PowerShell v3 or newer.
@AnsgarWiechers Thanks. @Hiten004 If PS 2.0 is being used, then then the order of the operands can be reversed and -notcontains can be used instead. "1919SMAHESHWARE","1919IETEST", "1920BPASCERITB" -notcontains $_.name
0

Please try below code

$old = (Get-Date).AddDays(-90) # Modify the -90 to match your threshold $oldComputers = Get-ADComputer -searchbase "OU=Workstations,DC=Corporate,DC=Local" -SearchScope 2 -Filter { PasswordLastSet -le $old } -Properties * $oldComputers = $oldComputers | where {$_.name -notlike "1919DD" ` -or $_.name -notlike "1919SMAHESHWARE" ` -or $_.name -notlike "1919IETEST" ` -or $_.name -notlike "1920BPASCERITB"} Export-Csv c:\temp\Over90DaysMachines.csv -NoTypeInformation -force -append 

2 Comments

It did not work @Hiten I get this continuous error : -or : The term '-or' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:6 char:6 + -or $_.name -notlike "1919IETEST" + ~~~ + CategoryInfo : ObjectNotFound: (-or:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
Try $oldComputers = $oldComputers | where {$_.name -notlike "1919DD" -or $_.name -notlike "1919SMAHESHWARE" -or $_.name -notlike "1919IETEST" -or $_.name -notlike "1920BPASCERITB"} on the same line or use backticks ` to break lines

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.