powershell script to get user details

ARM2009
ARM2009 used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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?

Author

Commented:
distinguishedname is in the input file
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

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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.

Author

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

Author

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
Glad that worked for you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial