Active directory Attribute

Hi,

Please I need a script (vbscript or Powershell)  to export the following attribute from two "OU" in active directory,
After to run the script we obtein a output file .csv (encode in UTF-8 )

I want to display in the output file the following fields:
"Account","Disabled","First Name","Last Name","Mail"

*Disable = True or False
"""ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local"",""DC1Serv.dom.local"""
"""ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local"",""DC1Serv.dom.local"""

Best regards,
DRRAMAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

yo_beeDirector of Information TechnologyCommented:
Here is my script

Import-Module ActiveDirectory
$DNArray = @('OU=Users,OU=Production,OU=XXX,DC=XXXXXX,DC=local','OU=HELPDESK,OU=Users,OU=Production,OU=XXX,DC=XXXXXX,DC=local')
$users = $null
Foreach ($dn in $dnarray)
{

$users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Properties Name,Enabled,GivenName,sn,mail | Select Name,Enabled,GivenName,sn,mail 

$users |Export-Csv -Path '.\AD.csv' -NoTypeInformation -force  

}

Open in new window


This it modified with your OU's

Import-Module ActiveDirectory
$DNArray = @('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local',,'ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')
$users = $null
Foreach ($dn in $dnarray)
{

$users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Properties Name,Enabled,GivenName,sn,mail | Select Name,Enabled,GivenName,sn,mail 

$users |Export-Csv -Path '.\AD.csv' -NoTypeInformation -force  

}

Open in new window

MilesLoganCommented:
This may also help

$OUs = 'OU ONE DN', 'OU TWO DN'
$(ForEach ($OU in $OUs) { Get-aduser -filter * -SearchBase $OU -Properties samaccountName, enabled, GivenName, SN, mail | Select-Object samaccountName, enabled, GivenName, SN, mail }) | Export-csv C:\Data\UserData.csv -nti

Open in new window

yo_beeDirector of Information TechnologyCommented:
@MilesLogan

I like seeing an alt method.
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

DRRAMAuthor Commented:
Hi,
Please can you modify the script because when I run the script; In the output file, I see the following fields
Name,Enabled,GivenName,sn,mail

But I need to display in the output file the following fields:
 "Account","Enabled","First Name","Last Name","Mail"
DRRAMAuthor Commented:
Please I am waiting your help.
yo_beeDirector of Information TechnologyCommented:
Not sure what you are referencing for the Account Attribute?
DRRAMAuthor Commented:
Hi,
when I run the script; In the output file, I need to see the following fields in display name (first line)

 "Account","Enabled","First Name","Last Name","Mail"

Best regards
yo_beeDirector of Information TechnologyCommented:
OK, but what value do you want to populate the Account column.
yo_beeDirector of Information TechnologyCommented:
Let me know if this works
Import-Module ActiveDirectory
$DNArray = @('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local',,'ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')
$users = $null
Foreach ($dn in $dnarray)
{

$users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Properties Name,Enabled,GivenName,sn,mail | Select @{n="Account";e={($_.name).ToLower()}},@{n="Disabled";e={($_.enabled)}},@{n="First Name";e={($_.GivenName)}},@{n="Last Name";e={($_.sn)}},@{n="EmailAddress";e={($_.mail).ToLower()}} 

$users |Export-Csv -Path '.\AD.csv' -NoTypeInformation -force  

}

Open in new window

DRRAMAuthor Commented:
Please I need to add the name of Domain controller on the folowing path

$DNArray = @('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local',,'ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')

Bes regards;
yo_beeDirector of Information TechnologyCommented:
You cannot get that attribute from the users attribute that are available.  If you mean the OU that holds the user or the full distinguished name (DN).  Here are both options.

Account = Distinguish Name

Import-Module ActiveDirectory
$DNArray = @('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local','ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')
$users = $null
Foreach ($dn in $dnarray)
{

$users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Properties Name,Enabled,GivenName,sn,mail,DistinguishedName | Select @{n="Account";e={($_.DistinguishedName).ToLower()}},@{n="Disabled";e={($_.enabled)}},@{n="First Name";e={($_.GivenName)}},@{n="Last Name";e={($_.sn)}},@{n="EmailAddress";e={($_.mail).ToLower()}} 

$users |Export-Csv -Path '.\AD.csv' -NoTypeInformation -force  

}

Open in new window


Here is the user's OU for the account column

Import-Module ActiveDirectory
$DNArray = @('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local','ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')
$users = $null
Foreach ($dn in $dnarray)
{

$users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Properties Name,Enabled,GivenName,sn,mail,DistinguishedName | Select @{n="Account";e={$User.DistinguishedName.Substring($User.DistinguishedName.IndexOf(",OU=")+1)}},@{n="Disabled";e={($_.enabled)}},@{n="First Name";e={($_.GivenName)}},@{n="Last Name";e={($_.sn)}},@{n="EmailAddress";e={($_.mail).ToLower()}} 

$users |Export-Csv -Path '.\AD.csv' -NoTypeInformation -force  

}

Open in new window

DRRAMAuthor Commented:
I want search the users from the two OUs  from the same DC server;
Where I can add the DC server in the code?????
yo_beeDirector of Information TechnologyCommented:
Do you have multiple domains in your environment?  If not it does not matter what DC you get the information from.
This attribute is not stored in the user's list of attributes.
DRRAMAuthor Commented:
I have One domain but I have some DC (Controller domain) for that I want to specify the DC server name
yo_beeDirector of Information TechnologyCommented:
If you have 3 DC's its a round robin for which one is used to query unless you use the -server switch for Get-ADuser, but not sure how this makes a difference.
If your domain is healthy then all data should have replicated to the other DC's.  So when you query your domain for a user it will be pulled from any one of your DC's

Import-Module ActiveDirectory
$DNArray = @ ('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local',,'ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')
$users = $null
Foreach ($dn in $dnarray)
{

$users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Server DC01-Properties Name,Enabled,GivenName,sn,mail,DistinguishedName | Select @{n="Account";e={$User.DistinguishedName.Substring($User.DistinguishedName.IndexOf(",OU=")+1)}},@{n="Disabled";e={($_.enabled)}},@{n="First Name";e={($_.GivenName)}},@{n="Last Name";e={($_.sn)}},@{n="EmailAddress";e={($_.mail).ToLower()}} 

$users |Export-Csv -Path '.\AD.csv' -NoTypeInformation -force  

}

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
yo_beeDirector of Information TechnologyCommented:
Following up to see if my last comment made sense?
DRRAMAuthor Commented:
thx
yo_beeDirector of Information TechnologyCommented:
Thanks for accepting my suggestion, but I am still curious on why you need the server.  By you adding the -Server switch it is implicated applied that it is coming from that server.   So if that is the case you can populate the table with a static value of <DC01>.  

It does not make sense to me the importance of this info.
DRRAMAuthor Commented:
Because we have some DCs in the world and I need to define two DCs (for example DCs of London city) when I run the script In office of London.
But I don't Know how can I add two DCs in the script?

Thx
yo_beeDirector of Information TechnologyCommented:
But is you have a single domain all your locations should have the same data.  If you have your Sites and Services setup properly for the different subnets then that would be the DC the clients are talking to. If you leave Sites and Services as the default and all your DC's are talking to all subnets then it is a round-robin to which DC the client is talking to.  

It sounds like you have a domain that is disjointed and not properly configured if you need to speak to a specific DC.  

So you are saying that LondonDC01 has different data that USADC01, but has the same OU's ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local ?
yo_beeDirector of Information TechnologyCommented:
DRRAMAuthor Commented:
Yes I agree
 Thx very much for your help
yo_beeDirector of Information TechnologyCommented:
You agree with what?
It seems to me that you might not fully understand how Active Directory works and is configured.  I am trying to understand your needs for the specific DC and if it is because the data is not the same at each DC then like I said you have a bigger problem on your hands.
DRRAMAuthor Commented:
Hi,

When I run the script on server 2008 R2;

I have the message error:

Error: Exception calling "LoadFile" with "1" argument(s): "This assembly is built by a runtime newer than the currently
loaded runtime and cannot be loaded. (Exception from HRESULT: 0x8013101B)"
yo_beeDirector of Information TechnologyCommented:
Do you have RSAT installed with Active Directory Powershell Module?
https://4sysops.com/archives/how-to-install-the-powershell-active-directory-module/
DRRAMAuthor Commented:
Ok thx,

But I have another error in the output file :

é=?
à=?

What is the resolution please
yo_beeDirector of Information TechnologyCommented:
Can you post your script and out as it should up on your screen?
DRRAMAuthor Commented:
the same script

Import-Module ActiveDirectory
$DNArray = @('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local',,'ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')
$users = $null
Foreach ($dn in $dnarray)
{

$users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Properties Name,Enabled,GivenName,sn,mail | Select @{n="Account";e={($_.name).ToLower()}},
@{n="Disabled";e={($_.enabled)}},
@{n="First Name";e={($_.GivenName)}},
@{n="Last Name";e={($_.sn)}},
@{n="EmailAddress";e={($_.mail).ToLower()}}

$users |Export-Csv -Path '.\AD.csv' -NoTypeInformation -force  

}

In Outputfile

First Name
Al?xandre

correct  : Aléxandre
yo_beeDirector of Information TechnologyCommented:
Do you have the Active Directory Powershell installed on the computer you are running this from?
DRRAMAuthor Commented:
yes I have
yo_beeDirector of Information TechnologyCommented:
You call have two ,, for the $DNARRAY
here is an adjusted script
Import-Module ActiveDirectory
 $DNArray = @('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local','ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')
 $users = $null
 Foreach ($dn in $dnarray)
 {

 $users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Properties Name,Enabled,GivenName,sn,mail | Select @{n="Account";e={($_.name).ToLower()}},
 @{n="Disabled";e={($_.enabled)}},
 @{n="First Name";e={($_.GivenName)}},
 @{n="Last Name";e={($_.sn)}},
 @{n="EmailAddress";e={($_.mail).ToLower()}} 

 $users |Export-Csv -Path '.\AD.csv' -NoTypeInformation -force  

 } 

Open in new window

DRRAMAuthor Commented:
yes I corrected before
yo_beeDirector of Information TechnologyCommented:
What is your Output result?
DRRAMAuthor Commented:
my output file is ok but just it converted the é or ç or à or other waracter with accent to this symbol  ?
yo_beeDirector of Information TechnologyCommented:
What language is your DC's set at ?
DRRAMAuthor Commented:
the problem isn't my DCs because when I use a vbscript I don't have a problem for accent
DRRAMAuthor Commented:
do you have any idea ?

please
yo_beeDirector of Information TechnologyCommented:
So you do have none English characters.
DRRAMAuthor Commented:
I don't have the frensh characters.
yo_beeDirector of Information TechnologyCommented:
Add the -Encoding UTF8 to the end of the Export-csv cmdlet

Import-Module ActiveDirectory
 $DNArray = @('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local','ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')
 $users = $null
 Foreach ($dn in $dnarray)
 {

 $users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Properties Name,Enabled,GivenName,sn,mail | Select @{n="Account";e={($_.name).ToLower()}},
 @{n="Disabled";e={($_.enabled)}},
 @{n="First Name";e={($_.GivenName)}},
 @{n="Last Name";e={($_.sn)}},
 @{n="EmailAddress";e={($_.mail).ToLower()}} 

 $users |Export-Csv -Path '.\AD.csv' -NoTypeInformation   -Encoding UTF8 -force  

 } 

Open in new window

DRRAMAuthor Commented:
very good thx;

Please, Can you explain me how I can change the separator between attribute in the output file from , to ;
DRRAMAuthor Commented:
yo_bee
please do you have any suggestions to change the separator ?
yo_beeDirector of Information TechnologyCommented:
add -delimiter ';'  inline with the export-csv  

Import-Module ActiveDirectory
 $DNArray = @('ou=Accounts,ou=Sales,ou=USA,dc=Dom,dc=local','ou=Accounts,ou=Marketing,ou=USA,dc=Dom,dc=local')
 $users = $null
 Foreach ($dn in $dnarray)
 {

 $users += Get-ADUser -filter * -SearchBase $dn -SearchScope OneLevel -Properties Name,Enabled,GivenName,sn,mail | Select @{n="Account";e={($_.name).ToLower()}},
 @{n="Disabled";e={($_.enabled)}},
 @{n="First Name";e={($_.GivenName)}},
 @{n="Last Name";e={($_.sn)}},
 @{n="EmailAddress";e={($_.mail).ToLower()}} 

 $users |Export-Csv -Path '.\AD.csv' -NoTypeInformation  -Delimiter ';' -Encoding UTF8 -force  

 } 

Open in new window

DRRAMAuthor Commented:
yo_bee
When I run the script, I obtain the following message

 Editing the execution strategy
The execution strategy can protect you against scripts that you consider unreliable. By modifying the
execution strategy, you expose yourself to the security risks described in the Help topic
about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the strategy
execution?
[O] Yes [T] Yes for all [N] No [U] No for all [S] Suspend [?] Help (default is "N"):

Open in new window

yo_beeDirector of Information TechnologyCommented:
Since you are running a script that is not signed you will get this message unless you change the policy to unrestricted.

The url in the message explains it all.
DRRAMAuthor Commented:
can you add the command in the script ? and hide the message?
DRRAMAuthor Commented:
yo_bee
please do you have any suggestions to change the code?
Thx
yo_beeDirector of Information TechnologyCommented:
No.  This is something that you need to set for your computer's Powershell environment.
You have a few option and one of them is to set to unrestricted
I have never put this in a script before.
This is what I did and then the setting is always what you just set.
Set-ExecutionPolicy unrestricted 

Open in new window

yo_beeDirector of Information TechnologyCommented:
DRRAMAuthor Commented:
I want to use :
Set-ExecutionPolicy bypass
yo_beeDirector of Information TechnologyCommented:
what happens when you set that
DRRAMAuthor Commented:
in windows 10 : error message to modify the registry key
in Windows 2008 R2 nothing
yo_beeDirector of Information TechnologyCommented:
If you run Powershell with elevated permission are you able to set it on the windows 10 machine and run your script?
DRRAMAuthor Commented:
yes it's ok when I run with admin account
yo_beeDirector of Information TechnologyCommented:
So what's the issue then.  This seems like it would be an admin task to run anyway.
DRRAMAuthor Commented:
great thx
yo_beeDirector of Information TechnologyCommented:
Sorry I was not able to give you better info, but this is how Powershell works.

Since this is closed others really do not pay much attention. I would recommend posting a follow up question with this script and see what others say.
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.