c# - C# 活动目录搜索
问题描述
我有这个 powershell 函数,我想把它变成一个 C# 函数。我怎样才能把它放到 C# 中?
Get-ADComputer -filter {Name -Like 'myComp'} -property * | select DistinguishedName
解决方案
你应该能够很容易地做到这一点。添加对System.DirectoryServices.AccountManagement
然后使用此代码的引用:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, 'YourDomain'))
{
ComputerPrincipal computer = ComputerPrincipal.FindByIdentity (ctx, "name");
if (computer != null)
{
// do whatever you need to do with your computer principal
string distinguishedName = computer.DistinguishedName;
}
}
更新:如果你不知道你的域名........ - 你也可以使用:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
在这种情况下,将为您所在的当前域创建主体上下文。
推荐阅读
- c# - 具有第 3 次集成的 3 层架构
- reactjs - 将 docker-compose 环境变量的值转换为服务 ip 地址
- angular - 输入装饰器不显示来自父组件的数据
- c# - 带有凭据的 C# WebProxy
- python - 以有效的方式加入具有格式的元组列表
- css - Side horizontal lines with flexbox
- objective-c - 带有objective-c的Json反序列化在版权符号上失败
- influxdb - 迭代 InfluxDB 中的大量数据
- git - Github:使用 ssh 密钥仅在一个存储库或组织中使用
- javascript - 除了两小时前更新的文件外,如何查找文件?