Link to home
Create AccountLog in
Avatar of ARM2009
ARM2009Flag for United States of America

asked on

powershell script to get user details

I have a input file with the distinguishednames in the format CN=...dc=com for a list of 300 users

looking to finalize the script below to get results.


Import-CSV C:\users.csv |

ForEach-Object {
     Get-QADUser $_.users -Properties * | Select samaccountname, Name, Userprincipalname, lastLogon, lastLogonTimestamp |
    Write-Host
    #Write-Host
    Export-Csv C:\users-info.csv
     }  

The export is not giving the right results... I guess its not appending to the file when looping for users. need to add an array?


any better way to do it?
Avatar of Bob McCoy
Bob McCoy
Flag of United States of America image

You have several things going on ...
You never pipe into the Export-CSV
Even if you did, Write-Host empties the pipeline
What do you have in the Users column in the CSV file?  SamAccountName?  DistinguishedName?
Avatar of ARM2009

ASKER

distinguishedname is in the input file
ASKER CERTIFIED SOLUTION
Avatar of Bob McCoy
Bob McCoy
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of ARM2009

ASKER

no errors but the output file is empty...
I'm getting output on mine.  Please check -- is it a true CSV file?  That is, is there a header row with a column title of "Users"?  Are the distinguished names surrounded by double quotes since they have embedded commas?  Are there extra spaces at the end of the DN?  Your data is suspect at this point.
Avatar of ARM2009

ASKER

I am using the same input file to see data using my original script without the export... so I know its reading the data right...

Import-CSV C:\users.csv |

 ForEach-Object {
      Get-QADUser $_.users -Properties * | Select samaccountname, Name, Userprincipalname, lastLogon, lastLogonTimestamp |
Write-Host
     #Export-Csv C:\users-info.csv
      }
I can't vouch for what Write-Host is doing to the data in the pipeline since its only purpose in life is to prepare the output for the screen.

Without several lines of your input data, I'd just be guessing.  And that doesn't help either one of us.
Avatar of ARM2009

ASKER

i removed write-host... ended up using your script and all went well. as you mentioned after a thorough check  the CSV format was not right and once fixed the script worked as expected. Thanks Bob
Glad that worked for you.