PowerShell的 - 过滤OU中同时使用GET-adcomputer [英] Powershell - Filtering OUs while using get-adcomputer

查看:1039
本文介绍了PowerShell的 - 过滤OU中同时使用GET-adcomputer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建,其基于其上的计算机可能具有特定属性的计算机列表的脚本。例如,我试图让Windows XP的计算机和Windows 7的计算机列表,抛出他们的名字在.csv文件输出每个最终计数。

I am trying to create a script that generates a list of computers based on specific properties which a computer may have. For example, I am trying to make a list of Windows XP computers and Windows 7 computers, throw their names in a .csv file and outputting the final count of each.

下面是我的code到目前为止

Here is my code so far

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不是寻找到更好的,但我不能完全弄清楚。

This script works however I would like to make it a bit better by being able to say which OU's not to look into, but I can't quite figure it out.

如果任何人有任何深入了解如何做到这一点,甚至只是为了使上述code任何更好,我很乐意听到的。

If anyone has any insight into how to do this, or even just to make the above code any better I would love to hear it.

感谢您!

推荐答案

运营商似乎不使用通配符的distinguishedName工作。因此,明显的操作 GET-ADComputer -Filter {(distinguishedName来-notlike* OU =邪恶*)} 不起作用。

The -like operator doesn't seem to work with wildcards for DistinguishedName. So the obvious operation Get-ADComputer -Filter {(DistinguishedName -notlike "*OU=evil,*")} doesn't work.

最简单的解决方法是让一个colleciton所有计算机并将其过滤之后,以满足您的需求。像现在这样,

The easiest workaround is to get all the computers in a colleciton and filter it afterwards to suit your needs. Like so,

# 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,*"}

附录

要结合匹配规则,使用 - 和 -or 。请记住,使用 * 通配符与? (如对象)

To combine matching rules, use -and -or and -like. Remember to use * wildcard with ? (where-object)

# All the computers save the ones from evil and wicked OU:
$goodOU = $computers| ? {
  $_.DistinguishedName -notlike "*ou=evil,*" -and $_.DistinguishedName -notlike "*ou=wicked,*"

}

这篇关于PowerShell的 - 过滤OU中同时使用GET-adcomputer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆