Welcome to Powergui.org - an open source community for Windows Powershell

PowerGUI.org PowerGUI.org and blogs

Forums » Active Directory and PowerShell

Thread: Empty fields when importing from CSV

This question is answered.


Permlink Replies: 5 - Pages: 1 - Last Post: Mar 8, 2010 1:58 AM by: Jitendra Kumar
karabatov

Posts: 2
Registered: 12/3/09
Empty fields when importing from CSV
Posted: Dec 3, 2009 5:52 AM
 
  Click to reply to this thread Reply

Dear All,

I'm trying to write a script for creating users in AD based on an import from CSV file.

The main point is that a number of fields in the file are EMPTY, and it looks like they should be processed correctly (according to http://dmitrysotnikov.wordpress.com/2008/10/07/skip-empty-csv-fields/).

In my case I get an error: The attribute syntax specified to the directory service is invalid. (Exception from HRESULT: 0x8007200B)

CSV file:
firstname,lastname,name,alias,mail,department,title,description,phone,password
Test,Tester,Tester Test,t.tester,,IT,Test administrator,IT,,87654321

Script excerpt:
$data = import-csv $args[0]
foreach ($i in $data)
{
New-QADUser -Service "server.domain.local" -ParentContainer "OU=Users,DC=domain,DC=local" -UserPassword $i.password -SamAccountName $i.alias -UserPrincipalName $upn -Name $i.name -City "Moscow" -Company "Company" -Department $i.department -Email $i.mail -FirstName $i.firstname -LastName $i.lastname -Office "Company" -PhoneNumber $i.phone -PostalCode "123456" -StreetAddress "Some St., 32" -Title $i.title -ObjectAttributes @{c="RU";co="Russia"} -Description $i.description -DisplayName $i.name | Enable-QADUser
}

Problem is that different users have different empty fields, and it's mighty inconvenient to build up a command based on the value of the field.

Is this correct behaviour for empty fields?


Andrey Moiseev (Quest)

Posts: 415
Registered: 9/4/07
Re: Empty fields when importing from CSV
Posted: Dec 7, 2009 8:17 AM   in response to: karabatov
Answered
  Click to reply to this thread Reply

It's a bug in AD Cmdlets 1.3. Reference number: 109860
String parameters in New-* cmdlets can't be $null or empty.
String parameters in Set-* cmdlets can't be empty (but can be $null).

This bug will be fixed in maintenance release. ETA is the end of January, 2010.

Workaround for Set-*: use $null values instead of "":
Example:
Use
Set-QADUser testUser -Description $null
instead of
Set-QADUser testUser -Description ""

Workaround for New-*: use New-* | Set-* combination.
Example:
Use
New-QADUser -ParentContainer my.domain.com/test -Name testUser | Set-QADUser -Description $null
instead of
New-QADUser -ParentContainer my.domain.com/test -Name testUser -Description $null




karabatov

Posts: 2
Registered: 12/3/09
Re: Empty fields when importing from CSV
Posted: Dec 7, 2009 10:15 AM   in response to: Andrey Moiseev ...
 
  Click to reply to this thread Reply

Thanks, Andrey, especially for a workaround.

Will be waiting for a fix!


butlerrc30

Posts: 3
Registered: 1/26/10
Re: Empty fields when importing from CSV
Posted: Jan 26, 2010 8:18 PM   in response to: karabatov
 
  Click to reply to this thread Reply

Any update on the release date? The $null method doesn't seem to work for me but if I use any character it works fine even a space.


rob_campbell@centraltechn...

Posts: 18
Registered: 3/9/07
Re: Empty fields when importing from CSV
Posted: Feb 24, 2010 4:54 PM   in response to: butlerrc30
 
  Click to reply to this thread Reply

Workaround:

$PathCSV = "test2.csv"
$OU = "OU=Users,DC=XYZ,DC=local"
#---

$users = Import-Csv $PathCSV
$props = $users[0] | gm -MemberType NoteProperty |? {$_.name -ne "Name"}

$users |%{
 $makenew = 'new-QADuser -Name $_.name -ParentOU  $OU'
 foreach ($prop in $props){
  if ($_.($prop.name)){$makenew += (" -" + $prop.name + " " + '$_.' + $prop.name)}
  }
 iex $makenew
}





Jitendra Kumar

Posts: 1
Registered: 3/8/10
Re: Empty fields when importing from CSV
Posted: Mar 8, 2010 1:58 AM   in response to: Andrey Moiseev ...
 
  Click to reply to this thread Reply

Is the fix ready for this?
Can we download it. If so, please provide the link.

Thanks,

Jitendra.




Legend
MVP: 2501 + pts
Guru: 2001 - 2500 pts
Expert: 751 - 2000 pts
Enthusiast: 31 - 750 pts
Novice: 0 - 30 pts
Moderators
Helpful answer (5 pts)
Answered (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums