Powershell中的条件运算符

时间:2017-10-31 10:28:46

标签: powershell

我正在尝试编写一个脚本,它将检查多个操作系统版本(例如示例6.1,5.1等),如果计算机是“启用=真”'在广告中 ;然后它将返回整个计算机列表及其在Excel工作表中的属性。

当我只使用一个条件来检查操作系统版本时,脚本运行正常。运行良好的脚本如下: -

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

然而,问题是当我使用'和'检查多个操作系统版本的条件然后脚本返回一个空白的Excel工作表。

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

任何人都可以请我指出我在这里做错了什么以及如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

除非你在它们之间放置-OR,否则你不能使用相同条件的两倍' OperatingsystemVersion'

如果您为同一个计算机对象搜索5.1和6.1,则不会得到任何结果。

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