我正在尝试创建一个脚本,该脚本根据计算机可能具有的特定属性生成计算机列表。例如,我试图列出 Windows XP 计算机和 Windows 7 计算机的列表,将它们的名称放入 .csv 文件中,并输出每台计算机的最终计数。
到目前为止,这是我的代码
import-module ActiveDirectory
$computers = get-adcomputer -Filter 'ObjectClass -eq "Computer"' -properties "OperatingSystem"
$i = 0
$j = 0
Foreach ($computer in $computers) {
if ($computer.operatingSystem -like "Windows 7*") {
$i++
'"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$computer.DistinguishedName" | Out-file -append C:\users\admin\desktop\test.txt
}
elseif ($computer.OperatingSystem -like "Windows XP*") {
$j++
'"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$computer.DistinguishedName" | Out-file -append C:\users\admin\desktop\test.txt
}
else {
$_
}
}
write-host "$i Win 7"
write-host "$j Win xp"
$k = $i+$j
write-host "$k Total"
示例输出:
104 Win 7
86 Win xp
190 Total
这个脚本可以工作,但是我想让它变得更好一点,能够说出哪个 OU 不值得研究,但我不太明白。
如果有人对如何做到这一点有任何见解,或者只是为了使上述代码变得更好,我很乐意听到。
谢谢你!
The -like
运算符似乎不适用于 DistinguishedName 的通配符。那么显而易见的操作Get-ADComputer -Filter {(DistinguishedName -notlike "*OU=evil,*")}
不起作用。
最简单的解决方法是获取集合中的所有计算机,然后对其进行过滤以满足您的需求。就像这样,
# All the computers from the evil OU:
$evilOU = $computers| ? {$_.DistinguishedName -like "*ou=evil,*"}
# All the computers but the ones from the evil OU:
$goodOU = $computers| ? {$_.DistinguishedName -notlike "*ou=evil,*"}
Addendum
要组合匹配规则,请使用-and
-or
and -like
。记得使用*
通配符与? (where-object)
# All the computers save the ones from evil and wicked OU:
$goodOU = $computers| ? {
$_.DistinguishedName -notlike "*ou=evil,*" -and $_.DistinguishedName -notlike "*ou=wicked,*"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)