简体   繁体   English

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

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

hi i want update the active directory with powershell and asp.net for this i use this code: 嗨,我想用powershell和asp.net更新活动目录,我使用此代码:

costCenter = "777"; 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
            {

            }
        }

this is the error: 这是错误:

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

How I can do this? 我怎么能这样做?

@{} is a powershell-shortcut to create a hashtable. @{}是创建哈希表的PowerShell快捷方式。 In C# you should create a hashtable in the normal C#-way. 在C#中,您应该在正常的C#-way中创建一个哈希表。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET中使用Powershell和System.Management.Automation更新字段extensionAttribute3? - How I can update the Field extensionAttribute3 with Powershell and System.Management.Automation in ASP.NET`? 如何使用C#按用户名搜索Active Directory? - How can I search Active Directory by username using C#? 如何在c#中查询Active Directory站点和服务? - How can I query Active Directory Sites and Services in c#? C#无法运行PowerShell脚本添加用户Active Directory - C# Can not run PowerShell script to add user Active Directory 如何在C#中的Active Directory中的UserPrincipal对象上设置管理器属性 - How do I set the Manager Attribute on the UserPrincipal object in Active Directory in C# C# Active Directory - 如何从注册服务目录 object 加载“certificateTemplates”属性? - C# Active Directory - How can I load the “certificateTemplates” Property from an Enrollment Services directory object? 使用c#的Active Directory属性列表 - Active Directory Attribute List Using c# 如何在c#中访问属性属性中的类变量? - How can I access to class variable in property attribute in c#? 我可以使用MVC 3在C#中使用Active Directory进行身份验证吗? - Can i do Authentication with Active Directory in C# with MVC 3? 如何从C#中的Active Directory获取自定义字段? - How can I get a custom field from Active Directory in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM