Assign the value to attribute with hyphen in Active Directory using PowerShell code

40 pts.
Tags:
Active Directory
PowerShell Scripts
Here is the code I am using to modify the attribute of the object class "User" in active directory. The attribute is "cvx-informationStewart". Can anyone help how to update the attribute that has hyphen as I am getting error.
.Csv data contains:
"samAccountName","informationSteward"
"!tqjz","tqjz"

Code :

Add-PSSnapin Quest.ActiveRoles.ADManagement

foreach ($record in (Import-Csv C:name.csv)) {
$user = $record.samAccountName
$command = “Set-QADUser -identity $user”

foreach ( $attr in (Get-Member -InputObject $record -MemberType NoteProperty) ) {
$value = $record.($attr.Name)
if ( $value -and ( $attr.Name -ne ‘samAccountName’ ) ) {
$command += ” -” + $attr.Name + ” ‘” + $value + ”’”
}
}
write-host $command
Invoke-Expression $command

}

Answer Wiki

Thanks. We'll let you know when a new response is added.

The command line you are looking for with Qwest tools would be:

Set-QADUser -Identity $user -objectAttributes @{‘cvx-informationStewart’=$value}

If you use builtin PS cmdlets, from the activedirectory module, it would be:
Set-ADUser -Identity $user -Replace @{cvx-informationStewart’=$value}

Discuss This Question: 7  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Sagar25

    Using ActiveRoles Management Shell

     

    foreach ($record in (Import-Csv C:\name.csv)) {
      $user = $record.samAccountName
      $command = Set-QADUser -Identity $user -objectAttributes @{‘cvx-informationStewart’=$value}

      foreach ( $attr in (Get-Member -InputObject $record -MemberType NoteProperty) ) {
         $value = $record.($attr.Name)
         if ( $value -and ( $attr.Name -ne ‘samAccountName’ ) ) {
     $command += ” -” + $attr.Name + ” ‘” + $value + ”’”
         }
       }

      write-host $command
      Invoke-Expression $command

    }

     

    Error received :

    Method invocation failed because [Quest.ActiveRoles.ArsPowerShellSnapIn.Data.ArsUserObject] doesn't contain a method
    named 'op_Addition'.
    At line:7 char:1
    + $command += " -" + $attr.Name + " `" + $value + "'"
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

    CT\!tqjz
    CT\!tqjz : The module 'CT' could not be loaded. For more information, run 'Import-Module CT'.
    At line:1 char:1
    + CT\!tqjz
    + ~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (CT\!tqjz:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CouldNotAutoLoadModule

    40 pointsBadges:
    report
  • karbonfybar
    To set multiple attributes with one command, it will have to look like this:
    Set-QADUser -Identity $user -objectAttributes @{‘cvx-informationStewart’=$value;$2ndAttributeName=$2ndValue;$3ndAttributeName=$3ndValue;}

    You are essentially creating a string with $commmand and PowerShell does not like doing "+" the way you are using it. You would need to start like this:
    $command = "Set-QADUser -Identity $user -objectAttributes @{‘cvx-informationStewart’=$value;"
    Then your $command variable would add to itself like this:
    $command += "$($attr.Name)=$value;"
    but then you would need a final line to be like this:
    $command += "}"
    35 pointsBadges:
    report
  • Sagar25

    Hi karbonfybar..Thanks for your help..But when tried the below command recieved error

    [PS] C:\>foreach ($record in (Import-Csv C:\name.csv)) {
    >>   $user = $record.samAccountName
    >>   $command = Set-QADUser -Identity $user -objectAttributes @{`cvx-informationStewart'=$value}
    >>
    >>   write-host $command
    >>   Invoke-Expression $command
    >>
    >> }
    >>

    Error :

    CT\!tqjz
    CT\!tqjz : The module 'CT' could not be loaded. For more information, run 'Import-Module CT'.
    At line:1 char:1
    + CT\!tqjz
    + ~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (CT\!tqjz:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CouldNotAutoLoadModule

     

    40 pointsBadges:
    report
  • karbonfybar
    What is CT\!tqjz? Is it data from the csv? Also, Invoke-Expression works with a string. $command is not a string. To make it a string you would need to do this: $command = "Set-QADUser -Identity ..." I think you would be better off just running your command: foreach($record in (Import-Csv c:\name.csv)){ Set-QADUser -Identity $record.SamAccountName -objectAttributes @{'cvx-informationStewart'=$value} } Of course you will need to set the value of $value
    35 pointsBadges:
    report
  • Sagar25

    My Active Directory Server is 2003 Native.

    So basically, I want to update the "cvx-informationStewart" attribute of the object for the mulitiple Users for example "!tqjz".

    But the object "!tqjz" and the value for the attribute is in .csv file. So need to import the .csv file.

    Can anyone help me how to use the standard ADSI code for this..

    40 pointsBadges:
    report
  • Genderhayes

    Try jsonObj['md-number']

    7,100 pointsBadges:
    report
  • SriniJoy
    Is "CT\!tqjz" specified in your csv file? Is CT the domain name and !tqjz is the user name? Try removing CT\ from the csv file and try again.

    Might be a good idea to run a Get-ADUser first for the record imported from the csv file to see if it exists.

    Change the code:  $user = $record.samAccountName
    to :
     $user = (Get-ADUser $record.samAccountName).samAccountName
    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following