簡體   English   中英

如何使用c#中的Powershell類更新Active Directory中的extensionAttribute3屬性?

[英]How I can update the extensionAttribute3 Attribute in Active Directory with Powershell class in c#?

嗨,我想用powershell和asp.net更新活動目錄,我使用此代碼:

costCenter =“777”;

public string SetADUser(string Auth_Password,string sAMAccountName, string CostCenter)
        {

            Security key = new Security();

            try
            {
                SecureString pw = new SecureString();

                foreach (char x in Auth_Password)
                {
                    pw.AppendChar(x);
                }

                InitialSessionState initial = InitialSessionState.CreateDefault();
                Environment.SetEnvironmentVariable("ADPS_LoadDefaultDrive", "0");
                initial.ImportPSModule(new string[] { "ActiveDirectory" });

                PSCredential crend = new PSCredential(ADNAME, pw);

                using (Runspace runspace = RunspaceFactory.CreateRunspace(initial))
                {
                    runspace.Open();
                    using (Pipeline p = runspace.CreatePipeline())
                    {
                        Command command = new Command("Set-ADUser");
                        command.Parameters.Add("Identity", sAMAccountName);
                        command.Parameters.Add("Replace","@{extensionAttribute3=" + "'" + CostCenter + "'" + "}"); ?? what is here wrong
                        //command.Parameters.Add("extensionAttribute3", CostCenter);
                        command.Parameters.Add("Credential", crend);

                        p.Commands.Add(command);

                        p.Invoke();

                        return string.Empty;

                    }
                }
            }
            catch (Exception ex)
            {

                return ex.Message;
            }
            finally
            {

            }
        }

這是錯誤:

The Parameter "Replace" can not bind. The Value "@{extensionAttribute3='777'}" from typ "System.String" can not convert to "System.Collections.Hashtable".

我怎么能這樣做?

@{}是創建哈希表的PowerShell快捷方式。 在C#中,您應該在正常的C#-way中創建一個哈希表。

var ht = new Hashtable { { "extensionAttribute3", CostCenter } };
command.Parameters.Add("Replace", ht);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM