由 Powershell 管理的过滤器

Filter Managedby Powershell

以下是我目前尝试拉取由 "ML..." 等同名管理的 AD 组的尝试。我不断收到错误,所以我想知道为什么我可以过滤 managedby“-eq $...”变量时却无法使用“-like”过滤 managedby。我尝试创建一个变量 $name = "ML*" 以便我可以执行 {managedby -eq $name} 但仍然没有运气。

我经常遇到这样的错误:

 Operator(s): The following: ''Eq', 'Ne'' are the only operator(s) suppor
ted for searching on extended attribute: 'ManagedBy'. 

等等,因为“-eq”只被我做过的一些过滤器接受。当我使用 -eq 时,出现以下错误:

Import-Module : The following error occurred while loading the extended type dat
a file: 
Microsoft.PowerShell, C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\ActiveD
irectory\ActiveDirectory.Types.ps1xml : File skipped because it was already pres
ent from "Microsoft.PowerShell".
Microsoft.PowerShell, C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\ActiveD
irectory\ActiveDirectory.Types.ps1xml : File skipped because it was already pres
ent from "Microsoft.PowerShell".

At J:\ManagedbyEqualsML.ps1:1 char:14
+ Import-Module <<<<  ActiveDirectory
+ CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeExc 
eption
+ FullyQualifiedErrorId : FormatXmlUpateException,Microsoft.PowerShell.Comm 
ands.ImportModuleCommand

The term 'Get-adgroup' is not recognized as the name of a cmdlet, function, scri
pt file, or operable program. Check the spelling of the name, or if a path was i
ncluded, verify that the path is correct and try again.
At J:\ManagedbyEqualsML.ps1:53 char:27
+  $MLgroupAll = Get-adgroup <<<<  -Properties managedby, enabled, name -filter 
{managedby -eq $name}
+ CategoryInfo          : ObjectNotFound: (Get-adgroup:String) [], CommandN 
otFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

这是我的代码,我试图在其中找到名称为 ML*

的所有者
Import-Module ActiveDirectory

$name = "ML*"

#Attempt 1

$MLgroups = Get-adgroup -Properties managedby, enabled, name -filter * | Select name, managedby

foreach ($group in $MLgroups){

if ($group.managedby -like "ML*"){
write-host $group.name + $group.managedby}

}

#Attempt 2

$Mgroups = get-adgroup -Properties name, managedby -filter *
 foreach ($groups in $Mgroups){
        # here get the group name and use the "managedBy attribute to retrieve the user object
        # grou naem
        $gname = $_.Name
        $manager=Get-AdUser $_.ManagedBy
        $MangerName = $manager.DisplayName

        if ($managerName -like "ML*"){
        write-host $gname + $managerName}

}

#Attempt 3

$exportlist = "C:\Temp\managedby.txt"

Clear-Content $exportlist


$Header = `
"Group ID Name" + "|" + "ManagedBy"

$Header | Out-File $exportlist -Append

$list = get-adgroup -properties name, managedby -filter {managedby -like "ML_*"} `
| Select name, managedby | Export-CSV $exportlist -NoType -Delimiter '|'

#Attempt 4

$MLgroupAll = Get-adgroup -Properties managedby, enabled, name -filter {managedby -like $name}
foreach ($group in $MLgroupAll) {

write-host $group.name + $group.managedby}

更新:如果我尝试更改我的 $name 变量,它仍然不起作用并给出另一个错误。

 $MLgroupAll = get-adgroup -Properties managedby, enabled, name -filter {managedby -eq $name}
foreach ($group in $MLgroupAll) {

$managed = $group.managedby
    if ($managed -like "ML*"){
write-host $group.name + $group.managedby }

}

错误: Get-ADGroup :扩展属性中提供的身份信息:'ManagedBy' coul d没有解决。原因:'找不到标识为 'ML*' 的对象:'D C=we,DC=dirsrv,DC=com'.'.

@Paul:我的错误仍然存​​在:

这是一个对我有用的例子(在你最后一次尝试时定位自己):

get-adgroup -filter * -Properties managedby | % { 

if($_.managedby -like "CN=ML*"){
write-host $_.name + $_.managedby
}

}