如何为Active Directory站点添加子网描述(c#)

时间:2014-08-12 14:04:50

标签: c# .net active-directory directoryservices

我正在创建一种创建Active Directory站点的方法。它可以很好地创建一个站点,添加子网,创建站点链接等...但我找不到添加子网描述的方法。

当我创建子网时,我这样做:

var contextType = new DirectoryContext(DirectoryContextType.Forest, "forest", "user","Password"]);
var site = System.DirectoryServices.ActiveDirectory.ActiveDirectorySite.FindByName(contextType, SiteCode);

foreach (string sn in Subnet)
{
    try
    {
        var subnet = new ActiveDirectorySubnet(contextType, sn, SiteCode);
        subnet.Location = Location;
        subnet.Save();

    }
    catch (Exception ex)
    {
        ...
    }
}

它将列表中的子网添加到站点,但我找不到添加说明的方法。

ActiveDirectorySubnet类似乎没有任何描述属性,但它位于" Active Directory站点和服务" UI ...

有谁知道在哪里保存这些信息?

2 个答案:

答案 0 :(得分:0)

这是一个PowerShell,但应该很容易转换为C#(此链接可能很有用http://support.microsoft.com/kb/315716

$object = [adsi]'LDAP://CN=192.0.2.0\/24,CN=Subnets,CN=Sites,CN=Configuration,DC=example,DC=com'  
$object.description = "Test-net"  
$object.CommitChanges()

; - P

答案 1 :(得分:0)

推动它。只需用字符串

播放一下

你可以使用它:

SetSubnetDescription( “LDAP://CN=10.197.6.128 \ / 25,CN =子网,CN =站点,CN =配置,DC =测试,DC =域”);

    public static  bool SetSubnetDescription(string Subnet)
    {
    try
    {
    DirectoryEntry ent = new DirectoryEntry(Subnet);
    Object ads = ent.NativeObject;
    Type type = ads.GetType();
    type.InvokeMember("Description", 
        BindingFlags.SetProperty, 
        null, 
        ads, 
        new object[] {"your new description"});

    // The changes to the object must always be committed or else they 
    // will be lost.
    ent.CommitChanges(); 
    return true;
    }
    catch (Exception ex)
    {
     MessageBox.Show(ex.ToString());
     return false;
    }