Search code examples
powershellfilterowner

Filter Managedby Powershell


Below are my current attempts to pull AD groups whose managedby equal names like "ML...". I keep getting errors so I wanted to know why I am unable to filter managedby with "-like" when I can filter managedby "-eq $..." variables. I tried making a variable $name = "ML*" so that I can perform {managedby -eq $name} but still had no luck.

I mostly get error like:

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

and so forth because "-eq" is only accepted for some filters I have done. When I use -eq I get these errors:

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

Here are my codes where I attempted to find Owners that have the name 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}

UPDATE: if i try to changed my $name variable it still doesn't work and gives another error.

 $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 }

}

ERROR: Get-ADGroup : Identity info provided in the extended attribute: 'ManagedBy' coul d not be resolved. Reason: 'Cannot find an object with identity: 'ML*' under: 'D C=we,DC=dirsrv,DC=com'.'.

@Paul: here is my error still:

enter image description here


Solution

  • Here is an example that works for me (orienting myself at your last try):

    get-adgroup -filter * -Properties managedby | % { 
    
    if($_.managedby -like "CN=ML*"){
    write-host $_.name + $_.managedby
    }
    
    }