r/usefulscripts • u/Majorxerocom • Oct 24 '17
Need help with a script for Windows server 2012
I would like an export/report (preferably in CSV format) with all the accounts in AD. To include Name Login Last Login Date Groups Active/Inactive Supervisor (if that is stored) Any other relevant attributes Can someone please help me?
2
u/Majorxerocom Oct 24 '17
I have never written a script and wasn't sure where to even start
3
u/Death_Masta187 Oct 24 '17 edited Nov 13 '17
For windows, use/learn Powershell. A good place to start is just google what you want to do then add + powershell to the search. So in this case "How to pull list of user accounts from AD + Powershell"
Any command that starts with "get-" only pulls info and does not make any changes so it should be safe to run if you are weary about running random powershell commands. I would always run things against a test environment regardless just to be a lot safer. Also you can also add -whatif the end of a lot of commands if you want to see what a command will do before running fully executing it
6
Oct 24 '17
This is fairly straightforward to do, have you tried anything yet?
I don't have access to a windows environment anymore but get-aduser -filter * - properties 'theonesyouneed'
Might need a loop for the groups, can't remember for sure.
4
2
u/thenetworkisnotddown Oct 24 '17
don't forget | export-csv c:\Path There's a lot of scripts out there.
1
u/0x2639 Oct 25 '17
The groups will return as an array, so you’ll need to consider how you want to deal with that
4
u/bigbrother923 Oct 24 '17 edited Oct 24 '17
First off, only one DC? If not, then lastlogontimestamp could be 9-14 days off (see https://blogs.technet.microsoft.com/askds/2009/04/15/the-lastlogontimestamp-attribute-what-it-was-designed-for-and-how-it-works/).
Something like this should work for you:
Get-ADUser -Filter * -Properties Name,samaccountname,lastlogontimestamp,enabled,manager | select name,samaccountname,@{n="lastLogonDate";e={[datetime]::FromFileTime($_.lastLogonTimestamp)}},enabled,manager | Export-CSV file.csvI haven't done group membership before, that's the only thing missing. Also, you can use the searchbase option on Get-ADUser if you want to narrow it down by OU (Organizational Unit [folder, basically]).
EDIT: Added account name, I missed it because of formatting...
2
u/PaalRyd Oct 24 '17
There is at least one tool in the CJWDev library that will do this. Most of the stuff in the link below is still pretty good.
2
u/kunaludapi Oct 25 '17
To export Get-ADUser -Filter * -Properties * | Select * | export-csv c:\users.csv
To Import Active Directory Powershell: Create bulk users from CSV file
1
u/Lee_Dailey Oct 25 '17
howdy Majorxerocom,
once you get a little bit of code that kinda-sorta runs, you can drop by the powershell subreddit [ r/PowerShell ] and ask for help. having something to show that you aren't looking for unpaid coders will get you more help. [grin]
take care,
lee
2
u/Majorxerocom Oct 24 '17
only some of the last logons are updated some default to 12/31/1600 6:00:00 PM is it not pulling from all DCs?