Avatar of ARM2009
ARM2009
Flag 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?
PowershellActive Directory

Avatar of undefined
Last Comment
Bob McCoy

8/22/2022 - Mon
Bob McCoy

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?
ARM2009

ASKER
distinguishedname is in the input file
ASKER CERTIFIED SOLUTION
Bob McCoy

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ARM2009

ASKER
no errors but the output file is empty...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Bob McCoy

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.
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
      }
Bob McCoy

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
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
Bob McCoy

Glad that worked for you.