by KieranBlenkarne at 2012-10-23 14:38:13
Hi guys,by DonJ at 2012-10-23 15:42:37
I exported a .csv file with samaccountname, department, description, title, company and office.
This worked fine.
I then go into the .csv, edit which fields/cells I would like to update in AD and then attempt to upload it with the following script I wrote, but it fails when it finds a blank cell and will not update.
I want it to basically notice if a cell is blank, leave it blank but upload the changes.
$data=import-csv c:\aduserlist.csv
foreach ($datarecord in $data)
{
if ($datarecord.sAMAccountName -ne ‘’){
$identity = $datarecord.sAMAccountName
}
else {
$identity = ‘’
}
if ($datarecord.company -ne ‘’){
$company = $datarecord.company
}
else {
$company = ‘’
}
if ($datarecord.department -ne ‘’){
$department = $datarecord.department
}
else {
$department = ‘’
}
if ($datarecord.description -ne ‘’){
$description = $datarecord.description
}
else {
$description = ‘’
}
if ($datarecord.title -ne ‘’){
$title = $datarecord.title
}
else {
$title = ‘’
}
if ($datarecord.office -ne ‘’){
$office = $datarecord.office
}
else {
$office = ‘’
}
set-ADuser -identity $identity -company $company -department $department -description $description -title $title -office $office
}
But any account with blank fields returns ( example below is for the Administrator account)]
set-aduser : replace
at line:37 char:11
+ set-ADuser <<<< -identity $identity -company $company -department $department -description $description -title $title -office $office
+categoryinfo : invalidoperation: (administrator:Aduser) [set-aduser]. adinvalidoperationexception
+fullyqualifiederrorid : replace,microsoft.activedirectory.management.commands.setaduser
Any help would be much appreciated!
So… you can’t do this the way you’re trying.by KieranBlenkarne at 2012-10-23 17:23:51
None of those parameters will accept ‘’ as a value. Essentially, if you don’t want to change something, you can’t specify the parameter at all.
Splatting is probably a better way to do this.
Here’s a basic look at how it works:
$params = @{‘Identity’=$identity}
This creates a basic hashtable with just your identity, since you’ll always know that. Then, decide if you want to add each parameter:
if ($datarecord.title -ne ‘’) {
$params.add(‘title’=$datarecord.title
}
That way, your hashtable only contains the items you actually want to change. Finally, run your command:
Set-ADUser @params
That’s probably the best way, given what you’re working with.
I am very new to PS, can you please help clarify a little more with using params?by DonJ at 2012-10-23 17:57:52
Well, I gave you the three bits of code you need. You just need to duplicate the second bit for each field in your CSV file. I did it with "title." If you need further clarification, you’re going to have to ask a specific question I can answer - I’m not sure what more to tell you at this point, though.by KieranBlenkarne at 2012-10-23 18:20:21
$params is just a variable name I made up.
if ($datarecord.title -ne ‘’) {by DonJ at 2012-10-23 18:29:38
$params.add(‘title’=$datarecord.title
}
Should this be
if ($datarecord.title -ne ‘’) {
$params.add(‘title’=$datarecord.title)
}
?
Actually, typo on my part. Sorry.
if ($datarecord.title -ne ‘’) {
$params.add(‘title’,$datarecord.title)
}
Sorry - I’ve been hectic today and missed those two typos. That should do it.