Den10102020
Den10102020

Reputation: 87

How I am able to pass the list of usernames on the foreach?

I am trying to make a loop from my variable $Unique_Groups but I keep getting the Get-ADObject: Variable: 'item' found in expression: $item is not defined. Maybe I did wrong on my filter side? Thank you so much in advance for the help.

$resultHOlder = @()
$data = Import-Csv -Path $path.csv 
# $data = Import-Csv -Path 
    foreach ($item in $data) {
        $table = new-object psobject
        $table | Add-Member -NotePropertyName Server_Name -NotePropertyValue  $item.'Server Name'
        $table | Add-Member -NotePropertyName Users_Group_Belonging -NotePropertyValue  $item.'User Name ( belonging to Group)'
        $table | Add-Member -NotePropertyName Account_Type -NotePropertyValue  $item.'Account Type'
        $table | Add-Member -NotePropertyName Machine_Domain -NotePropertyValue  $item.'Machine Domain'
        $table | Add-Member -NotePropertyName Account_Category -NotePropertyValue  $item.'Account Category'
        $table | Add-Member -NotePropertyName Account_Disabled -NotePropertyValue  $item.'AccountDisabled'
        $table | Add-Member -NotePropertyName Last_Login_Date -NotePropertyValue  $item.'Last Login Date'
        $table | Add-Member -NotePropertyName Domain_Name -NotePropertyValue  $item.'User Name ( belonging to Group)'.Split("\")[0]
        $table | Add-Member -NotePropertyName Object_Name -NotePropertyValue  $item.'User Name ( belonging to Group)'.Split("\")[1]
        $table | Where-Object { !$item.'User Name ( belonging to Group)'.contains("ONEABBOTT") } | ForEach-Object { $table.Domain_Name = $null }
        $resultHOlder += $table
    }

#    $Unique_Groups
$Unique_Groups =  $resultHOlder.Object_Name | sort -Unique
    
    
    
    # function to get the objecttype of the unique groups 
    function Get-ADobjectType {
        $storage = @()
        
        # $Unique_Groups | ForEach-Object {
            foreach ($item in $Unique_Groups) {
                # $filter = {"sAMAccountName -eq ""`$_"""} 
                $Membertype = Get-adobject -Filter {sammaccountName -eq $item}
                $out = new-object psobject
                $out | add-member noteproperty AD.localAdminMember $item
                if($Membertype.Name -ne $null){$out | add-member noteproperty AD.Object $Membertype.Name}else{$out | add-member noteproperty AD.Object "NODATA"}
                if($Membertype.ObjectClass -ne $null){$out | add-member noteproperty AD.Class $Membertype.ObjectClass}else{$out | add-member noteproperty AD.Class "NODATA"}
                $storage += $out
                
               $out
            # }   
        }
        return 
    }
    Get-ADobjectType

I am expecting for a result like this:

AD.localAdminMember               AD.Object AD.Class
-------------------               --------- --------
user1                         user1     user
user2                         user2     user
group1                        group1    group
group2                        group2    group

Upvotes: 1

Views: 331

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60045

Ok, you can use something like this:

function Get-ADObjectType {
[cmdletbinding()]
param(
    [parameter(mandatory,valuefrompipeline)]
    [string]$Name
)

    begin
    {
        $domain = (Get-ADRootDSE).DefaultNamingContext
    }

    process
    {
        # foreach ($item in $Unique_Groups) { <= Don't need this, process block handles this for you

        $filter = "(|(name=$Name)(samAccountName=$Name))"
        $object = Get-ADObject -LDAPFilter $filter
            
        if(-not $object)
        {
            [pscustomobject]@{
                YourInput = $Name
                ObjectName = "Not found in $domain"
                ObjectClass = $null
            }
            return
        }

        [pscustomobject]@{
            YourInput = $Name
            ObjectName = $object.Name
            ObjectClass = $object.objectClass
        }
    }
}

Use it like this if you want to process multiple users:

  • $resultHOlder.Object_Name | sort -Unique | Get-ADObjectType
  • 'this.Example.User1','this.Example.User2','this.Example.User3' | Get-ADObjectType

This function will stream results, meaning, each user will be evaluated and sent to standard output.

For a unique user you can do:

  • Get-ADObjectType -Name this.Example.User

Edit:

I think this is something, not related to your function, but related to making your life easier in the future :)

All this:

$resultHOlder = @()
$data = Import-Csv -Path $path.csv 
# $data = Import-Csv -Path 
foreach ($item in $data) {
    $table = new-object psobject
    $table | Add-Member -NotePropertyName Server_Name -NotePropertyValue  $item.'Server Name'
    $table | Add-Member -NotePropertyName Users_Group_Belonging -NotePropertyValue  $item.'User Name ( belonging to Group)'
    $table | Add-Member -NotePropertyName Account_Type -NotePropertyValue  $item.'Account Type'
...
...
...

Can be replaced with this:

$header = @(
    'YourHeaderName 1'
    'YourHeaderName 2'
    'YourHeaderName 3'
    'YourHeaderName 4'
    'YourHeaderName 5'
)

$csv = Get-Content -Path $path |
       Select-Object -Skip 1 |
       ConvertFrom-Csv -Header $header

Upvotes: 1

Related Questions