将 LDAP/AD 脚本从 VBS 重构为 C#

发布于 2024-11-30 19:43:53 字数 2035 浏览 3 评论 0原文

我需要将处理 LDAP、ADODB 和 ActiveDirectory 的 VBS 从 VBS 重构为 C#。我卡住的部分是连接(刚刚开始并且已经卡住了......太棒了)。这是原始来源

Set objRootDSE = GetObject("LDAP://RootDSE")
strConfig = objRootDSE.Get("configurationNamingContext")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"

adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

strQuery = "<LDAP://dm1.com.local/OU=CN,OU=Users,OU=CMS Organizational,OU=CMS_Users_and_Groups,DC=cms,DC=local>;(&(objectCategory=person)(objectClass=user)(!useraccountcontrol:1.2.840.113556.1.4.803:=2));distinguishedName,lastLogon,whenCreated;subtree"

adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 60
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

C# 看起来像这样

DirectoryEntry dse = new DirectoryEntry("LDAP://RootDSE");
string config = dse.Properties["configurationNamingContext"].Value.ToString();
string domain = dse.Properties["defaultNamingContext"].Value.ToString();
Connection connection = new Connection();
connection.Provider = "ADsDSOObject";
connection.Open("ADsDSOObject", "", "", 0);

object records, parameters = "";

ADODB.Command command = new Command();
command.ActiveConnection = connection;
command.CommandText = "<LDAP://dm1.com.local/OU=CN,OU=Users,OU=CMS Organizational,OU=CMS_Users_and_Groups,DC=cms,DC=local>;(&(objectCategory=person)(objectClass=user)(!useraccountcontrol:1.2.840.113556.1.4.803:=2));distinguishedName,lastLogon,whenCreated;subtree";
command.Execute(out records, ref parameters, 0);

它给了我错误

Interface not supported (Provider)
 at ADODB.CommandClass.Execute(Object& RecordsAffected, Object& Parameters, Int32 Options)
at Adug.Program.Main(String[] args) in E:\...\Program.cs:line 66

I need to refactor a VBS dealing with LDAP, ADODB and ActiveDirectory from VBS to C#. The part I'm stuck is connecting (just starting and already stuck... great). This is the original source

Set objRootDSE = GetObject("LDAP://RootDSE")
strConfig = objRootDSE.Get("configurationNamingContext")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"

adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

strQuery = "<LDAP://dm1.com.local/OU=CN,OU=Users,OU=CMS Organizational,OU=CMS_Users_and_Groups,DC=cms,DC=local>;(&(objectCategory=person)(objectClass=user)(!useraccountcontrol:1.2.840.113556.1.4.803:=2));distinguishedName,lastLogon,whenCreated;subtree"

adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 60
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

And the C# looks like this

DirectoryEntry dse = new DirectoryEntry("LDAP://RootDSE");
string config = dse.Properties["configurationNamingContext"].Value.ToString();
string domain = dse.Properties["defaultNamingContext"].Value.ToString();
Connection connection = new Connection();
connection.Provider = "ADsDSOObject";
connection.Open("ADsDSOObject", "", "", 0);

object records, parameters = "";

ADODB.Command command = new Command();
command.ActiveConnection = connection;
command.CommandText = "<LDAP://dm1.com.local/OU=CN,OU=Users,OU=CMS Organizational,OU=CMS_Users_and_Groups,DC=cms,DC=local>;(&(objectCategory=person)(objectClass=user)(!useraccountcontrol:1.2.840.113556.1.4.803:=2));distinguishedName,lastLogon,whenCreated;subtree";
command.Execute(out records, ref parameters, 0);

It gives me the error

Interface not supported (Provider)
 at ADODB.CommandClass.Execute(Object& RecordsAffected, Object& Parameters, Int32 Options)
at Adug.Program.Main(String[] args) in E:\...\Program.cs:line 66

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑忘罢 2024-12-07 19:43:53

我没有通过 ADO 查询 LDAP 的经验,但我已经成功使用了以下代码(此处进行了简化),该代码利用了 DirectorySearcher

DirectoryEntry directoryEntry = new DirectoryEntry(
      config.DirectoryConnectionString, 
      config.ActiveDirectoryUserName, 
      config.GetPassword(), 
      AuthenticationTypes.Secure);
DirectorySearcher ds = new DirectorySearcher(directoryEntry);

ds.PropertiesToLoad.Add("cn");
ds.PropertiesToLoad.Add("sAMAccountName");
ds.PropertiesToLoad.Add("mail");
ds.PropertiesToLoad.Add("displayName");

ds.Filter = "(objectClass=user)";

foreach (SearchResult result in ds.FindAll())
{
    string displayName = String.Empty;
    DirectoryEntry entry = result.GetDirectoryEntry();
    if (entry.Properties.Contains("displayName"))
            if (entry.Properties["displayName"].Count > 0)
                displayName  = entry.Properties["displayName"][0].ToString();
}

I have no experience with querying LDAP via ADO, but I have used the following code successfully (simplified here), which makes use of DirectorySearcher:

DirectoryEntry directoryEntry = new DirectoryEntry(
      config.DirectoryConnectionString, 
      config.ActiveDirectoryUserName, 
      config.GetPassword(), 
      AuthenticationTypes.Secure);
DirectorySearcher ds = new DirectorySearcher(directoryEntry);

ds.PropertiesToLoad.Add("cn");
ds.PropertiesToLoad.Add("sAMAccountName");
ds.PropertiesToLoad.Add("mail");
ds.PropertiesToLoad.Add("displayName");

ds.Filter = "(objectClass=user)";

foreach (SearchResult result in ds.FindAll())
{
    string displayName = String.Empty;
    DirectoryEntry entry = result.GetDirectoryEntry();
    if (entry.Properties.Contains("displayName"))
            if (entry.Properties["displayName"].Count > 0)
                displayName  = entry.Properties["displayName"][0].ToString();
}
巡山小妖精 2024-12-07 19:43:53

使用 System.DirectoryServices 命名空间是访问 .NET 中 Active Directory 的首选方法。 Daniel B 的回答应该会让您朝着正确的方向前进。

Using the System.DirectoryServices namespace is the preferred method for accessing Active Directory in .NET. Daniel B's answer should get you moving in the right direction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文