Answered by:
CSV AttributeEditing empty Value's

Question
-
Hello,
I've been working on a simple script to edit several users in AD which require me to add some data in extensionattribute fields,
Not every user has data which requires this field to be filled in to my script runs if there is data present in the csv file with the correct header. However when i leave the field empty the script refuses the run, (see code below & CSV loadout below)The question is how can i run my script that so that it will continu on even if the extensionattribute fields are left empty in the csv file. Can't seem to get it to run with out data
Import-Module ActiveDirectory $users = Import-Csv -Path \adtest.csv foreach ($user in $users) {Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" | Set-ADUser -title $($user.title) ` -streetAddress $($user.streetAddress) ` -OfficePhone $($user.OfficePhone) ` -HomePage $($user.HomePage) ` -MobilePhone $($user.Mobile) ` -PostalCode $($user.postalCode) ` -City $($user.city) ` -Add @{"extensionattribute1"=$user.extensionattribute1;"extensionattribute2"=$user.extensionattribute2} ` }
extensionattribute1,samaccountname,extensionattribute2,title,OfficePhone,Mobile,HomePage,streetAddress,postalCode,country,city Dr,bd-tst-usr,Eng,Tile Of User Here, +123456789,+12346579 13,www.website.nl,StreetName 01,1234 AB , The Netherlands,City
Wednesday, March 20, 2019 12:15 PM
Answers
-
What about something like this?
Import-Module ActiveDirectory
$users = Import-Csv -Path \adtest.csv
foreach ($user in $users)
{
try {
$Props = @{}
if ($user.extensionattribute1) {$Props.extensionattribute1 = $user.extensionattribute1}
if ($user.extensionattribute2) {$Props.extensionattribute2 = $user.extensionattribute2}Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -ErrorAction Stop | Set-ADUser -title $($user.title) `
-ErrorAction Stop `
-streetAddress $($user.streetAddress) `
-OfficePhone $($user.OfficePhone) `
-HomePage $($user.HomePage) `
-MobilePhone $($user.Mobile) `
-PostalCode $($user.postalCode) `
-City $($user.city) `
-Add $Props
}
catch {
# you'll probably want a better exception message here
Throw "Didn't find user '$user' or couldn't update user"
}
}
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Edited by Rich Matheisen [Ex-MVP (retired)] Wednesday, March 20, 2019 3:19 PM
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Thursday, March 21, 2019 6:34 AM
- Marked as answer by bdiepeveen Monday, March 25, 2019 10:20 AM
Wednesday, March 20, 2019 3:14 PM
All replies
-
Hi
Have you tried adding a test for your variables?
if ($user.Extensionattribute1) { $Extensionattribute1= $user.Extensionattribute1 } else { $Extensionattribute1 = $null }
Wednesday, March 20, 2019 1:44 PM -
I know the script works and it can read the value's if entered
extensionattribute1,samaccountname,extensionattribute2,title,OfficePhone,Mobile,HomePage,streetAddress,postalCode,country,city Dr,bd-tst-usr,Eng,Tile Of User Here, +123456789,+12346579 13,www.website.nl,StreetName 01,1234 AB , The Netherlands,City
The above csv layout will work,
this will not (extensionattribute1 and extensionattribute2 are left empty as they do not contain value's )
extensionattribute1,samaccountname,extensionattribute2,title,OfficePhone,Mobile,HomePage,streetAddress,postalCode,country,city ,bd-tst-usr,,Tile Of User Here, +123456789,+12346579 13,www.website.nl,StreetName 01,1234 AB , The Netherlands,City
If thats what you mean by testing the variables then yes i know the work when presented with a value.
Wednesday, March 20, 2019 2:06 PM -
What about something like this?
Import-Module ActiveDirectory
$users = Import-Csv -Path \adtest.csv
foreach ($user in $users)
{
try {
$Props = @{}
if ($user.extensionattribute1) {$Props.extensionattribute1 = $user.extensionattribute1}
if ($user.extensionattribute2) {$Props.extensionattribute2 = $user.extensionattribute2}Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -ErrorAction Stop | Set-ADUser -title $($user.title) `
-ErrorAction Stop `
-streetAddress $($user.streetAddress) `
-OfficePhone $($user.OfficePhone) `
-HomePage $($user.HomePage) `
-MobilePhone $($user.Mobile) `
-PostalCode $($user.postalCode) `
-City $($user.city) `
-Add $Props
}
catch {
# you'll probably want a better exception message here
Throw "Didn't find user '$user' or couldn't update user"
}
}
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Edited by Rich Matheisen [Ex-MVP (retired)] Wednesday, March 20, 2019 3:19 PM
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Thursday, March 21, 2019 6:34 AM
- Marked as answer by bdiepeveen Monday, March 25, 2019 10:20 AM
Wednesday, March 20, 2019 3:14 PM -
This code seems to work,
did a little bit of fine tuning around the rest and your suggestion fixes the issue is counterend
Thank you for the assistance
Monday, March 25, 2019 10:21 AM