Matthias Nagele
Matthias Nagele

Reputation: 61

How to use "-replace" in Set-ADUser?

I want to use PowerShell via my C# to modify Active Directory attributes. Here is my PowerShell command which I can use to replace an Active Directory attribute:

Set-ADUser -Identity "kchnam" -Replace @{extensionAttribute2="Neuer Wert"}

How can I add the @{extensionAttribute2="Neuer Wert"} to my C# command?

My solution is not working:

Command setUser = new Command("Set-ADUser");
setUser.Parameters.Add("Identity", aduser);
string testadd = "@{extensionAttribute2=" + quote + "Neuer Wert" + quote + "}";
setUser.Parameters.Add("Replace", testadd);

Upvotes: 4

Views: 2773

Answers (1)

user4003407
user4003407

Reputation: 22132

In PowerShell that:

@{extensionAttribute2="Neuer Wert"}

means a Hashtable literal, not just string. So, in C# you also have to create a Hashtable object:

new Hashtable{{"extensionAttribute2","Neuer Wert"}}

Although, that is not fully equivalent to PowerShell, since PowerShell create Hashtable with case insensitive key comparer. But, very likely, that you can use any collection implementing IDictionary, not just a Hashtable.

Upvotes: 2

Related Questions