Avik Chowdhury
Avik Chowdhury

Reputation: 51

Conditional Operator in Powershell

I am trying to write a Script which will check for multiple OS version (like example 6.1 , 5.1 etc.) and if the Computer is 'Enabled = True' in AD ; then it will return the entire Computer list along with it's properties in an excel sheet.

When I am using only a single condition to check for the OS version then the script works fine. The script which runs fine is as follows :-

import-module ac*

Get-ADComputer -filter { OperatingSystemVersion -Like '*6.1*' -and Enabled -eq "true"} -SearchBase 'OU=MIComputers,OU=MI,dc=imaje,dc=intra' -Properties '*' | Select Name,OperatingSystem,Status, OperatingSystemVersion, `
LastLogonDate,CanonicalName | Export-Csv -NoType "C:\Temp\B2.csv" -Encoding UTF8

However, the issue is when I am using the 'and' condition to check for multiple OS version then the script returns a blank excel sheet.

import-module ac*

Get-ADComputer -filter { OperatingSystemVersion -Like '*6.1*' -and OperatingSystemVersion -Like '*5.1*' -and Enabled -eq "true"} -SearchBase 'OU=MIComputers,OU=MI,dc=imaje,dc=intra' -Properties '*' | Select Name,OperatingSystem,Status, OperatingSystemVersion, `
LastLogonDate,CanonicalName | Export-Csv -NoType "C:\Temp\B2.csv" -Encoding UTF8

Can anyone please point me to what I am doing wrong here and how this can be rectified ?

Upvotes: 1

Views: 868

Answers (1)

Vladimir Bundalo
Vladimir Bundalo

Reputation: 655

You cannot use two times the same condition 'OperatingsystemVersion', unless you put -OR between them.

If you search for both 5.1 and 6.1 for the same Computer Object, you will get no result.

import-module ac*

Get-ADComputer -filter { OperatingSystemVersion -Like '*6.1*' -OR OperatingSystemVersion -Like '*5.1*' -and Enabled -eq "true"} -SearchBase 'OU=MIComputers,OU=MI,dc=imaje,dc=intra' -Properties '*' | Select Name,OperatingSystem,Status, OperatingSystemVersion, `
LastLogonDate,CanonicalName | Export-Csv -NoType "C:\Temp\B2.csv" -Encoding UTF8

Upvotes: 4

Related Questions