youpa
asked on
Export Active Directory Users Attribute of all users in a specify location
Hello,
Anyone know a tools where I can set in a windows schedule task that will export in a CSV the First name & Last Name & Email Address & Display name of all the user located in a specific OU.
I have a Domain Control running Windows Server 2008
thanks
Anyone know a tools where I can set in a windows schedule task that will export in a CSV the First name & Last Name & Email Address & Display name of all the user located in a specific OU.
I have a Domain Control running Windows Server 2008
thanks
change the names as per your naming convention and configure below command in scheduled task
dsquery user "OU,OUname,DC=domain,DC=local" -limit 0 | dsget user -display -email -pager>c:\Users.csv
Command below will export all attributes for users:
Dsquery * -limit 0 -filter "&(objectClass=User)(objec tCategory= Person)" -attr * >> output.txt
Dsquery * -limit 0 -filter "&(objectClass=User)(objec
For exporting this can be done manually...
This scripts is tested scripts...
Hope it helps you.
Import-Module ActiveDirectory
#Create a variable for the date stamp in the log file
$LogDate = get-date -f yyyyMMddhhmm
#Sets the OU to do the base search for all user accounts, change for your env.
$SearchBase = "OU=****,DC=****,DC=com"
#Create an empty array for the log file
$LogArray = @()
$Users = @(Import-Csv "e:\temp\Leavers.csv")
ForEach ($User In $Users)
{
$Filter = "(company=" + $User."Employee ID" + ")"
$Return = Get-ADUser -searchbase $SearchBase -LDAPFilter $Filter
#Create new object for logging
If ($Return){
#Amend the users description
set-aduser $Return -Description ("Identified Leaver - Disabled on "+(get-date).toshortdatest ring())
#Disables user object.
Disable-ADAccount $Return
#Moves the account
Move-ADObject $Return -TargetPath "OU=Users,OU=****,OU=Inact ifs,DC=*** *,DC=com"
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $Return.name
$obj | Add-Member -MemberType NoteProperty -Name "Account Name" -Value $Return.samaccountname
$obj | Add-Member -MemberType NoteProperty -Name "Employee ID" -Value $User."Employee ID"
$obj | Add-Member -MemberType NoteProperty -Name "DistinguishedName" -Value $Return.DistinguishedName
#Adds object to the log array
$LogArray += $obj
}
}
#Exports log array to HTML file in the temp directory with a date and time stamp in the file name.
$a = "<style>"
$a = $a + "BODY{background-color:#D0 D0D0;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:#CC 0000}"
$a = $a + "TD{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:#D0 D0D0}"
$a = $a + "</style>"
$logArray | ConvertTo-HTML -head $a -body "<H2>Leavers Report</H2>"| Out-File "\\*****\Reports\Users_Lea ver_Report _$logDate. html "
##Creates and sends completion email with information##
$emailFrom = "AD-Scripts@*****.com"
$emailTo = "****.Admins@***.com"
$subject = "AD Users Leaver Script Complete"
$smtpServer = "*****"
$body = "The monthly AD Leavers script has run.`r`n"+
"`r`n"+
"`r`n"+
"Report is located \\*****\Reports\User_Leave rs_Report_ $logDate.h tml"
Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -SmtpServer $smtpServer
Thanks.
This scripts is tested scripts...
Hope it helps you.
Import-Module ActiveDirectory
#Create a variable for the date stamp in the log file
$LogDate = get-date -f yyyyMMddhhmm
#Sets the OU to do the base search for all user accounts, change for your env.
$SearchBase = "OU=****,DC=****,DC=com"
#Create an empty array for the log file
$LogArray = @()
$Users = @(Import-Csv "e:\temp\Leavers.csv")
ForEach ($User In $Users)
{
$Filter = "(company=" + $User."Employee ID" + ")"
$Return = Get-ADUser -searchbase $SearchBase -LDAPFilter $Filter
#Create new object for logging
If ($Return){
#Amend the users description
set-aduser $Return -Description ("Identified Leaver - Disabled on "+(get-date).toshortdatest
#Disables user object.
Disable-ADAccount $Return
#Moves the account
Move-ADObject $Return -TargetPath "OU=Users,OU=****,OU=Inact
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $Return.name
$obj | Add-Member -MemberType NoteProperty -Name "Account Name" -Value $Return.samaccountname
$obj | Add-Member -MemberType NoteProperty -Name "Employee ID" -Value $User."Employee ID"
$obj | Add-Member -MemberType NoteProperty -Name "DistinguishedName" -Value $Return.DistinguishedName
#Adds object to the log array
$LogArray += $obj
}
}
#Exports log array to HTML file in the temp directory with a date and time stamp in the file name.
$a = "<style>"
$a = $a + "BODY{background-color:#D0
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:#CC
$a = $a + "TD{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:#D0
$a = $a + "</style>"
$logArray | ConvertTo-HTML -head $a -body "<H2>Leavers Report</H2>"| Out-File "\\*****\Reports\Users_Lea
##Creates and sends completion email with information##
$emailFrom = "AD-Scripts@*****.com"
$emailTo = "****.Admins@***.com"
$subject = "AD Users Leaver Script Complete"
$smtpServer = "*****"
$body = "The monthly AD Leavers script has run.`r`n"+
"`r`n"+
"`r`n"+
"Report is located \\*****\Reports\User_Leave
Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -SmtpServer $smtpServer
Thanks.
ASKER
This command work fine , however it's only giving the display name, I would like also to have First Name and Lastname.
dsquery user "OU,OUname,DC=domain,DC=lo cal" -limit 0 | dsget user -display -email -pager>c:\Users.csv
thanks
dsquery user "OU,OUname,DC=domain,DC=lo
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks a lot for the points and glad to help. Just a heads up next time you can also split points for several helpful answers.
Thanks
Mike
Thanks
Mike
ASKER
Thanks all
Thanks
Miks