Question : Powershell - Comparison Operators with Multiple Values

Hi,

I am writing a command with Powershell to export all users in Active Directory that haven't logged into the domain for longer than a month and who are also not in particular OU's.

I am using the Quest cmdlets to achieve this.

The snippet I have attached works fine, but I want to compare 2 values (depts). The snippet will not show any users in the Sales dept, but I also want to add another comparison which I am having difficulty getting to work.

This is how I think it would work

Get-QADUser -sizelimit 0 | Where-Object { ($_.lastLogonTimeStamp -le $userlimit) -and ($_.DN -notlike "*sale*" -or "*maternity*") } | Select samAccountName, lastLogonTimeStamp, dn | Export-CSV "c:\userLogin.csv" -notypeinformation

or

Get-QADUser -sizelimit 0 | Where-Object { ($_.lastLogonTimeStamp -le $userlimit) -and ($_.DN -notlike "*sale*")  -or ($_.DN -notlike "*maternity*") } | Select samAccountName, lastLogonTimeStamp, dn | Export-CSV "c:\userLogin.csv" -notypeinformation

Your help would be appreciated!

Thanks
1:
Get-QADUser -sizelimit 0 | Where-Object { ($_.lastLogonTimeStamp -le $userlimit) -and ($_.DN -notlike "*sale*") } | Select samAccountName, lastLogonTimeStamp, dn | Export-CSV "c:\userLogin.csv" -notypeinformation

Answer : Powershell - Comparison Operators with Multiple Values

Hi

I think I cracked it, so simple and I missed the trick completely! I just added in multiple Where-Object cmd's.

Get-QADUser -sizelimit 0 | Where-Object {$_.lastLogonTimeStamp -le $userlimit} | Where-Object {$_.DN -notlike "*sale*"} | Where-Object {$_.DN -notlike "*maternity*"} | Select samAccountName, lastLogonTimeStamp, dn | Export-CSV "c:\userLogin.csv" -notypeinformation

There maybe a more streamline, program friendly way of doing this, but it works!

Cheers
Random Solutions  
 
programming4us programming4us