我正在使用VB.net,尝试查询Active Directory以检查并查看是否存在打印机。我有一个AD连接,但它在运行代码时似乎没有返回任何值。这是我的代码片段
Dim searchResults As New ArrayList
Dim myDirectorySearcher As New DirectorySearcher(myDirectoryEntry))
Dim targetObject as string = "printerName"
Dim searchFilter as string = "cn"
Dim strFilter = "(&(objectClass=printer)(" & searchFilter & "=" & targetObject & "))"
myDirectorySearcher.Filter = strFilter
myDirectorySearcher.CacheResults = False
For i = 0 To searchCriteria.Count - 1
myDirectorySearcher.PropertiesToLoad.Add(searchCriteria(i).ToString)
Next
Dim mySearchResult As SearchResult = myDirectorySearcher.FindOne()
尝试了各种方法,但似乎没有任何工作,任何建议都会非常感激。
答案 0 :(得分:4)
我必须做一些与我正在工作的项目类似的事情。简而言之,我认为您可能会在ActiveDirectory中的错误objectClass
下搜索。
打印机有时会添加到printQueue
下。
您的代码将类似于:
Dim searchResults As New ArrayList
Dim myDirectorySearcher As New DirectorySearcher(myDirectoryEntry))
Dim targetObject as string = "printerName"
Dim strFilter = "(&(objectClass=printQueue)(cn=" & targetObject & "))"
myDirectorySearcher.Filter = strFilter
myDirectorySearcher.CacheResults = False
For i = 0 To searchCriteria.Count - 1
myDirectorySearcher.PropertiesToLoad.Add(searchCriteria(i).ToString)
Next
Dim mySearchResult As SearchResult = myDirectorySearcher.FindOne()
值得注意的是,有时printerName
会将域附加到结尾,因此您的查询可能无法始终返回您期望的结果。
例如,您的打印机名称可能为PRINTER-RECEPTION
,但您的域名中会引用PRINTER-RECEPTION.MYCOMPANY.DOMAIN
。
希望这会对你有所帮助。