1

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 ?

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks for the pointer. The script works fine now. Also I had a query. The Script takes more than 20 minutes to run. Is this because of the number of machines in the AD or can the script be optimized ?
Yes, it could be. you could try to assign a variable to your search and then check how many records do you have with the Count method. $a = Get-ADComputer -filter { OperatingSystemVersion -Like '6.1' -or OperatingSystemVersion -Like '5.1' -and Enabled -eq "true"} -SearchBase "OU=EUD_DB7,DC=db,DC=demo" $a.count
Generally, @AvikChowdhury, you want to filter on the left, as early as you can. You're doing that in this code. However, the search itself is going to take a long time. You're looking for every computer with an OS like 6.1 or 5.1, which means a wildcard search. Thats going to take a while.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.