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

本文关键字:描述 子网 添加 站点 Active Directory | 更新日期: 2023-09-27 18:17:34

我正在创建一个创建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类似乎没有任何描述属性,但它存在于"ActiveDirectory站点和服务"UI中…

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

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

这是一个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

改进它。用string

弹奏一下

你可以这样写:

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;
    }