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?
ARM2009Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob McCoyCommented:
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?
ARM2009Author Commented:
distinguishedname is in the input file
Bob McCoyCommented:
Add-PSSnapin -Name Quest.ActiveRoles.ADManagement
Import-CSV -Path C:\ephemeral\users.csv | foreach {
    Get-QADUser -Identity $PSItem.users | 
        select -Property samaccountname, Name, Userprincipalname, lastLogon, lastLogonTimestamp 
} | Export-Csv -Path C:\ephemeral\users-info.csv -NoTypeInformation

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

ARM2009Author Commented:
no errors but the output file is empty...
Bob McCoyCommented:
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.
ARM2009Author Commented:
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 McCoyCommented:
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.
ARM2009Author Commented:
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 McCoyCommented:
Glad that worked for you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.