简体   繁体   中英

Get-AdUser no acepting variable

im trying to pull out all the information regarding my domain admin adminsitrators

#set domains we are going to query  
$domains = 'mydomainname.com'

#first here i bring the sam accounts names

    Foreach ($domain in $domains)
    {
        $OUTPUT =Get-AdGroupMember  -identity “Domain Admins” -recursive -server  $domain  |
                 Select-Object -Property samAccountName|
                 Select samAccountName;

        $Outputs +=$OUTPUT;
        $OUTPUT |Export-CSV "C:\File\$($domain).csv" -NoTypeInformation ;

    }
    $OUTPUT #this print the sam accounts 


   

#here is the problem 

    Foreach ($user in $OUTPUT)
   { 
        $Users2 =Get-ADUser -Filter "SamAccountName -like '$OUTPUT'" -Properties *  
        $USER3 +=$Users2;
        $Users2 |Export-CSV "C:\File\$($domain)Userpop.csv" -NoTypeInformation ;
    }

I think this is a problem with your filter. Try changing that line as follows:

Get-ADUser -Filter " SamAccountName -like `"$($user.samaccountname)`" " -Properties *

It can't be $OUTPUT as that's an array. Your loop variable is $user which is an object so you need the .samaccountname property. The filter needs a string with the -like matching a quoted string, so you need `" to pass the quote through to the final string.

Your CSV output of $user2 may not be what you expect either as each object is output to the same file. Perhaps you mean to have a -Append or write them to different files?

You'll probably want to reset $user3 as well. Perhaps add $user3 = @() before that loop.

You should always try to avoid adding to arrays with $array += $something , because that means the entire array gets rebuild in memory, costing time and resources.

Also, I would advise using more descriptive variable names, so the code will still be understandable after some time.

Then, because you are getting info from different domains, it is important to store the domain name in the first loop together with the samaccount names, so you can use these as -Server parameter in the second loop on Get-ADUser

Try

#set domains we are going to query  
$domains = @('mydomainname.com')  # since there is only one domain listed, use @() to force it into an array

$domainsAndAdmins = foreach ($domain in $domains) {
    # store the SamAccountNames for this domain as objects in an array
    $admins = Get-AdGroupMember -Identity 'Domain Admins' -Recursive -Server $domain  |
              Select-Object -Property SamAccountName
    # export this to csv file
    $outFile = 'C:\File\{0}.csv' -f $domain
    $admins | Export-Csv $outFile -NoTypeInformation

    # output an object with both the domain and the array of SamAccountNames
    # this will be captured in variable $domainsAndAdmins
    [PsCustomObject]@{Domain = $domain; Admins = $admins.SamAccountName }
}

# output on screen
$domainsAndAdmins | Format-Table -AutoSize

# will result in something like
#
# Domain                 Admins                    
# ------                 ------                    
# mydomainname.com       {jdoe, jbloggs, mpimentel}
# myseconddomainname.com {jdoe, mpimentel}   

# next get ALL (?) properties from the users we found
$domainsAndAdmins | ForEach-Object {
    $domain = $_.Domain
    $result = foreach ($user in $_.Admins) {
        Get-ADUser -Filter "SamAccountName -like '$user'" -Server $domain -Properties * -ErrorAction SilentlyContinue
    }
    $outFile = 'C:\File\{0}_Userpop.csv' -f $domain
    $result | Export-Csv $outFile -NoTypeInformation
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM