gpt4 book ai didi

c# - 如何确定我使用的机器是否是域 Controller

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:31 26 4
gpt4 key购买 nike

使用 C#,如何确定运行我的代码的机器是否是域 Controller 。

我不想“出去”并收集有关域中任何其他域 Controller 的信息...我只对运行我的代码的机器是否是域 Controller 感兴趣。此外,我不需要知道它是否是主域 Controller ...只要它是域 Controller 即可。

这是我尝试过的

试验 1

    private bool IsDomainController()
{
Domain domain = Domain.GetCurrentDomain();
string domainName = domain.ToString();
bool bIsDC = false;
DirectoryContext ctx = new DirectoryContext(DirectoryContextType.Domain, domainName);

try
{
using (DomainController dc = DomainController.FindOne(ctx, LocatorOptions.ForceRediscovery))
{
bIsDC = true;
}
}
catch (Exception)
{
bIsDC = false;
}

return bIsDC;
}

试验 2

    public bool IsThisMachineIsADomainController()
{
Domain domain = Domain.GetCurrentDomain();

string thisMachine = String.Format("{0}.{1}", Environment.MachineName, domain.ToString());
thisMachine = thisMachine.ToLower();

//Enumerate Domain Controllers
List<string> allDcs = new List<string>();

string name = "";
foreach (DomainController dc in domain.DomainControllers)
{
name = dc.Name.ToLower();
allDcs.Add(name);
}
return allDcs.Contains(thisMachine);
}

这些是否合适,为什么或为什么不合适?

最佳答案

经过大量研究...有两种方法:

适用于 Windows 2008 服务器:

    public bool isActiveDirectoryDomainServicesInstalled()
{
bool bRetval = false;
try
{
uint uID = 110;
string search = string.Format("SELECT * FROM Win32_ServerFeature WHERE ID = {0}", uID);
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher("root\\CIMV2", search);

foreach (var oReturn in oSearcher.Get())
{
if ((uint)(oReturn["ID"]) == uID)
{
bRetval = true;
break;
}
}
}
catch (Exception)
{
bRetval = false;
}

return bRetval;
}

.和.

适用于 Windows 2008 和 Windows 2012 服务器

public bool IsThisMachineIsADomainController()
{
Domain domain = Domain.GetCurrentDomain();

string thisMachine = String.Format("{0}.{1}", Environment.MachineName, domain.ToString());
thisMachine = thisMachine.ToLower();

//Enumerate Domain Controllers
List<string> allDcs = new List<string>();

string name = "";
foreach (DomainController dc in domain.DomainControllers)
{
name = dc.Name.ToLower();
allDcs.Add(name);
}
return allDcs.Contains(thisMachine);
}

关于c# - 如何确定我使用的机器是否是域 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25555495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com