Link to home
Start Free TrialLog in
Avatar of SAM IT
SAM IT

asked on

Need to script to pull out the report from Active directory

Hello Frnz,

Need to script to pull out the report from Active directory , Script has to pull out only users created & disabled from last 8 days with below mentioned fields, any help please

NTAccountName      ParentContainer      Email      AccountIsDisabled      Manager      SamAccountName      DN      UserPrincipalName      CreationDate      co      City      Office      PhoneNumber
Avatar of Alex
Alex
Flag of United Kingdom of Great Britain and Northern Ireland image

Get-ADUser -LDAPFilter "(&(objectClass=user)(objectCategory=person))" -Properties givenname,surname,name,employeeid,DistinguishedName,canonicalname,lastlogondate,PasswordExpired,PasswordLastSet,PasswordNeverExpires,PasswordNotRequired,EmailAddress,manager,Whencreated,whenchanged -SearchBase "PUT your DN in here" | export-csv c:\work\Test.csv
 

Open in new window


That will pull more than you need but you can filter it by the created dates in excel :-)

P.S The best way to build this sort of script is to do a get-aduser "Your username" -properties * and then you can pick from the column on the left :-)
Avatar of SAM IT
SAM IT

ASKER

I am sorry, this wont works for my requirement , But I got the similar script for my requirement below mentioned one I need to add the fields like NTAccountName      ParentContainer      Email      AccountIsDisabled      Manager      SamAccountName      DN      UserPrincipalName      CreationDate      co      City      Office      PhoneNumber

Script:

Get-ADUser -Filter * -Properties Modified | Where {$_.Modified -ge $(Get-Date).AddDays(-1)}
There is no AD attribute where the "Disabled" date is stored; this will only be traced if you have AD auditing enabled, and then you'd have to retrieve it from the security event log.
Modified/WhenChanged includes any changes, which includes attributes like Lastlogon which will be changed without an administrator doing anything manually.
You can obviously pipe the results further to Sort-Object and/or Export-Csv as required.
$FilterDate = (Get-Date).AddDays(-8).Date
Get-ADUser -Filter {(whenCreated -ge $FilterDate) -or (whenChanged -ge $FilterDate)} -Property UserPrincipalName, Enabled, Mail, l, physicalDeliveryOfficeName, telephoneNumber, co, Manager, whenCreated, whenChanged |
	Select-Object -Property SamAccountName, UserPrincipalName, Enabled, WhenCreated, WhenChanged, Mail, @{n='City'; e={$_.l}}, physicalDeliveryOfficeName, TelephoneNumber, @{n='Country'; e={$_.co}}, Manager, DistinguishedName, @{n='ParentContainer'; e={($_.DistinguishedName -split '(?<!\\),', 2)[1]}}

Open in new window

Avatar of SAM IT

ASKER

Perfectly worked, If I add NTAccountName in to this script, its given error, please assist
There is no attribute "NTAccountName"; SamAccountName (the pre-Windows-2000 logon name) is probably what you mean, and that's already included.
Avatar of SAM IT

ASKER

please see the below mentioned the difference between NTAccountName & SamAccountName

NTAccountName:  Domain\username

SamAccountName : username
$FilterDate = (Get-Date).AddDays(-8).Date
$NetBIOSDomain = (Get-ADDomain).NetBIOSName
Get-ADUser -Filter {(whenCreated -ge $FilterDate) -or (whenChanged -ge $FilterDate)} -Property UserPrincipalName, Enabled, Mail, l, physicalDeliveryOfficeName, telephoneNumber, co, Manager, whenCreated, whenChanged |
	Select-Object -Property SamAccountName, @{n='NTAccountName'; e={"$($NetBIOSDomain)\$($_.SamAccountName)"}}, UserPrincipalName, Enabled, WhenCreated, WhenChanged, Mail, @{n='City'; e={$_.l}}, physicalDeliveryOfficeName, TelephoneNumber, @{n='Country'; e={$_.co}}, Manager, DistinguishedName, @{n='ParentContainer'; e={($_.DistinguishedName -split '(?<!\\),', 2)[1]}}

Open in new window

Avatar of SAM IT

ASKER

Thanks for excellent script and Would like to know that I want to enance my skills in powershell  please suggest is there any tranings to imporve the skill set. Thanks in advance
Can't recommend any, sorry; was all "learning by doing" for me.
Avatar of SAM IT

ASKER

Script is pulling out the created users in 2006, need users created in last 8 days with below attribute
______________________-------------------------------------------------------
$FilterDate = (Get-Date).AddDays(-8).Date
$NetBIOSDomain = (Get-ADDomain).NetBIOSName
Get-ADUser -Filter {(whenCreated -ge $FilterDate) -or (whenChanged -ge $FilterDate)} -Property UserPrincipalName, Enabled, Mail, l, physicalDeliveryOfficeName, telephoneNumber, co, Manager, whenCreated, whenChanged |
      Select-Object -Property SamAccountName, @{n='NTAccountName'; e={"$($NetBIOSDomain)\$($_.SamAccountName)"}}, UserPrincipalName, Enabled, WhenCreated, WhenChanged, Mail, @{n='City'; e={$_.l}}, physicalDeliveryOfficeName, TelephoneNumber, @{n='Country'; e={$_.co}}, Manager, DistinguishedName, @{n='ParentContainer'; e={($_.DistinguishedName -split '(?<!\\),', 2)[1]}} | Export-Csv C:\Users\nsumanth\Desktop\testnew.csv
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM IT

ASKER

whenChanged states if any Object is modified in the AD its is considered as whenChanged
Yes, that's what I said in https:#a42035589
You mentioned in https:#a42035580 "I got the similar script for my requirement below mentioned", with the reference to Modified/WhenChanged, so I added the whenChanged.
Avatar of SAM IT

ASKER

Best response....