Solved

Script to change ad office attribute bulk

Posted on 2016-10-19
10
43 Views
Last Modified: 2016-10-19
i am currently using the below

Import-Module Activedirectory
Import-csv c:\input.csv | %{Set-ADUser $_.User -Office $_.Office}

but i would also like to update the address field in a different script or combine them how would i go about this?
0
Comment
Question by:Jorge Ocampo
  • 5
  • 4
10 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41850203
The relevant attributes are -City -Country -POBox -PostalCode -State -StreetAddress
Just make sure that your csv contains the respective columns, and set the ones you need using the correct header names (the header names don't have to match the AD attribute names, but it's easier to handle):
Import-Module Activedirectory
Import-csv c:\input.csv | % {Set-ADUser $_.User -Office $_.Office -City $_.City -StreetAddress $_.StreetAddress}

Open in new window

0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41850270
@oBdA for the streetaddresss do you know how i can create the following

1254 german blvd
STE 125A

instead of 1254 german blvd STE 125A
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41850294
Basically by adding <backtick>r<backtick>n where you want the line break.
Set-ADUser -Identity jdoe -StreetAddress "1254 german blvd`r`nSTE 125A"

Open in new window

The question is how your csv file is created and whether line breaks are supported. Powershell doesn't mind during import, as long as the fields are properly enclosed in double quotes. If not, you need some other "line break" character (let's say '|'), which will then be replaced during import:
Set-ADUser $_.User -StreetAddress $_.StreetAddress.Replace('|', "`r`n")

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 14

Expert Comment

by:Todd Nelson
ID: 41850296
Assuming this question is related to your other question (https://www.experts-exchange.com/questions/28976541/Script-to-change-ad-office-attribute-bulk.html).

If you can, create a single CSV file.  If you need to get information from AD to create appropriate headers for a CSV, run this command (for a single user...

Get-ADUser -Identity "USERNAME" -Properties *

Open in new window


NOTE: There is a assumption you are running these commands from PowerShell on a DC and have already run the command to import the AD module (Import-Module ActiveDirectory).

Review the parameters provided on the left of the screen.  Those can essentially be your headers for the CSV.  Given that, these are the headers you could use.  But like OBDA said, they do not have to match.

  • StreetAddress
  • POBox
  • City
  • State
  • PostalCode
  • Country

A sample CSV could look like this...

Name,Office,StreetAddress,POBox,City,State,PostalCode,Country
User1,Bakersfield,123 Street Lane,,Bakersfield,CA,12345,United States
User2,Barstow,234 Street Ave,,Barstow,CA,23456,United States

Open in new window


At this point, you can use the same CSV file in a single script or multiple scripts (as requested).

If a single script outside of the prior one you have then it will look something like this...

Import-Module ActiveDirectory
Import-Csv "C:\Users.csv" | ForEach-Object { Set-ADUser -Identity $_.Name -StreetAddress $_.StreetAddress -POBox $_.POBox -City $_.City -State $_.State -PostalCode $_.PostalCode -Country $_.Country }

Open in new window


But you can remove any of the parameters and CSV mapping you don't need.  For example, if you don't want to edit/add P.O. Box or Country then remove them from the command, like this...

Import-Module ActiveDirectory
Import-Csv "C:\Users.csv" | ForEach-Object { Set-ADUser -Identity $_.Name -StreetAddress $_.StreetAddress -City $_.City -State $_.State -PostalCode $_.PostalCode }

Open in new window


If you want them combined with your prior script, just add the Office parameter and the CSV map back into the command, like this...

Import-Module ActiveDirectory
Import-Csv "C:\Users.csv" | ForEach-Object { Set-ADUser -Identity $_.Name -Office $_.Office -StreetAddress $_.StreetAddress -City $_.City -State $_.State -PostalCode $_.PostalCode }

Open in new window


Hoe that helps.
0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41850309
the csv i am doing is comma delimited trying tried the first example and no go
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41850329
The "`r`n" can't be part of the string in the csv; that was just an example for a PS string containing line breaks.
In the csv, there would have to be a real Chr(13)Chr(10) combination for this to work without a replacement character:
"User","StreetAddress"
"jdoe","1254 german blvd
STE 125A"

Open in new window

If this can't be done, decide on a character that's not used otherwise, and use the replace method above.
0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41850334
gotcha thanks :)
0
 
LVL 2

Author Comment

by:Jorge Ocampo
ID: 41850412
one last question any idea why i am only getting the first result written to the csv? if i do command without export i get all results but when i add export only write out first result and stops.

$users = Get-Content C:\Users\user\Desktop\move.txt

foreach ($user in $users){
    $SplitName = -split $user
    Get-ADUser -Filter "(GivenName -eq '$($SplitName[0])') -and (Surname -eq '$($splitName[1])')" |Select-Object name, samaccountname | Export-CSV C:\Users\USER\Desktop\FILE.csv -NoTypeInformation
}
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41850439
Because you're exporting inside the loop. Since PS 3.0, Export-Csv supports -Append, but that's not really fast.
You can solve that with a subexpression:
$users = Get-Content C:\Users\user\Desktop\move.txt

$(foreach ($user in $users){
	$SplitName = -split $user
	Get-ADUser -Filter "(GivenName -eq '$($SplitName[0])') -and (Surname -eq '$($splitName[1])')" |
		Select-Object name, samaccountname
}) | Export-CSV C:\Users\USER\Desktop\FILE.csv -NoTypeInformation

Open in new window

0
 
LVL 2

Author Closing Comment

by:Jorge Ocampo
ID: 41850907
Amazing Help
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question