Question : PowerShell Active Directory

Hi there,

I try to query AD using the Quest ActiveRoles Mgmt Shell - the query works fine, but I can't pipe the results into a textfile. Here is what I do:

$Users=gc C:\tmp\GetADAccountInfos\Users.txt

foreach ($user in $users){
get-qaduser $User | format-table samaccountname,disabled,description,memberof | set-content C:\tmp\query.txt
}

or another try:


$Users=gc C:\tmp\GetADAccountInfos\Users.txt
f
oreach ($user in $users){
get-qaduser $User | select-object samaccountname,disabled,description,memberof | export-csv C:\tmp\query.csv
}

What is wrong? Thx buddies

Answer : PowerShell Active Directory


Remember that ForEach does not return objects to the pipeline where ForEach-Object does.

You can work-around the problem if the ForEach loop is executed within a script block. Although conversion to ForEach-Object is generally easier on the eyes.


Get-Content c:\tmp\GetADAccountInfos\Users.txt | ForEach-Object {
  Get-QADUser $_ | Select-Object SamAccountName, Disabled, Description, MemberOf
} | Export-Csv c:\tmp\query.csv


You might still have the same problem with System.String[]. If you do you need to teach PS how to deal with the array. One option is this:


Get-Content c:\tmp\GetADAccountInfos\Users.txt | ForEach-Object {
  Get-QADUser $_ | Select-Object SamAccountName, Disabled, Description,
    @{n='MemberOf';e={ "$($_.MemberOf)" }}
} | Export-Csv c:\tmp\query.csv


It uses the default Output Field Separator, a space, to join the string array (System.String[]) so you can see the contents. You can override the character used to join by setting a value for the reserved variable $OFS (e.g. $OFS = ";").

HTH

Chris
Random Solutions  
 
programming4us programming4us