Author Posts

November 23, 2015 at 2:56 am

Hey folks,
i have a problem with powershell script:
I would like to clear some User attributes with the following command:
Set-ADUser -Identity $samaccountname -Clear $clear

The $clear came from following if construct:
if (!($txtbox_city."Text" -like "")) { $replace['l'] = $txtbox_city."Text" } else { $clear = "'l',"}
if (!($txtbox_country."Text" -like "")) { $replace['co'] = "Germany" } # else { $clear += "'co'," }
if (!($txtbox_cc.Text -like "")) {$replace['countryCode'] = $codepage }
if (!($txtbox_abt3."Text" -like "")) { $replace['o'] = $txtbox_abt3."Text" } else { $clear += "'o'," }
if (!($txtbox_dn."Text" -like "")) { $replace['Displayname'] = $txtbox_dn."Text" } else { $clear += "'DisplayName'," }
if (!($txtbox_amt."Text" -like "")) { $replace['Department'] = $txtbox_amt."Text" } else { $clear += "'Department'," }
if (!($txtbox_desc."Text" -like "")) { $replace['Description'] = $txtbox_desc."Text" } else {$clear += "'Description'," }
if (!($txtbox_referat."Text" -like "")) { $replace['Division'] = $txtbox_referat."Text" } else { $clear += "'Division'," }
if (!($txtbox_eid."Text" -like "")) { $replace['employeeID'] = $txtbox_eid."Text" } else { $clear += "'employeeID'," }
if (!($txtbox_pnr."Text" -like "")) { $replace['EmployeeNumber'] = $txtbox_pnr."Text" } else { $clear += "'EmployeeNumber'," }

When i do this i became the error:
System.ArgumentExceptionThe specified directory service attribute or value does not exist
Parameter name: 'l','o','Description','Division','employeeID','EmployeeNumber','EmployeeType','facsimileTelephoneNumber'

When I do it in the powershell the same command, its working fine.

What can i do?

Thx

November 24, 2015 at 12:38 am

Hi,

The Clear parameter accepts string[] with is an array of strings, what you are doing is creating a single string and separating the attributes with ',' .

To fix the code you need to initialize the $clear variable like this:

$clear = @()

This will create it as an empty array.

To add the desired attributes to $clear:

$clear += 'l'

Please lets us know if it works.