PowerShell广告查询与C#广告查询-速度
我在学C#,我是个新手。请对我耐心点 我在C#中开发了一个应用程序,通过将PowerShell命令导入其中来搜索广告中的用户、组和组成员 现在,我正试图在C#中使用DirectoryServices来获得相同的结果,但是,返回相同结果所需的时间比在PowerShell中要长得多 下面是我现在使用DirectoryServices进行的快速测试:PowerShell广告查询与C#广告查询-速度,c#,powershell,active-directory,C#,Powershell,Active Directory,我在学C#,我是个新手。请对我耐心点 我在C#中开发了一个应用程序,通过将PowerShell命令导入其中来搜索广告中的用户、组和组成员 现在,我正试图在C#中使用DirectoryServices来获得相同的结果,但是,返回相同结果所需的时间比在PowerShell中要长得多 下面是我现在使用DirectoryServices进行的快速测试: using System.DirectoryServices; using System.DirectoryServices.ActiveDirecto
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
private void button1_Click(object sender, EventArgs e)
{
string textbox = textBox1.Text.ToString();
listBox1.Items.Clear();
listView1.Items.Clear();
listView1.Columns.Clear();
try
{
// Bind to the object for which to retrieve property data.
DirectoryEntry de = new DirectoryEntry("");
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectClass=Group)(cn="+ textbox + "))";
ds.SearchScope = SearchScope.Subtree;
SearchResultCollection rsAll = ds.FindAll();
listView1.Columns.Add("samsaccountname");
string samsaccountname = "";
foreach (SearchResult searchresult in rsAll)
{
if (searchresult.GetDirectoryEntry().Properties["samaccountname"].Value != null)
{ samsaccountname = searchresult.GetDirectoryEntry().Properties["samaccountname"].Value.ToString(); }
else { samsaccountname = ""; }
ListViewItem lvi = new ListViewItem(samsaccountname);
//lvi.SubItems.Add(givenName);
//lvi.SubItems.Add(sn);
//lvi.SubItems.Add(mail);
listView1.Items.Add(lvi);
}
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
catch
{
// Add error handling.
}
}
以下是我在PowerShell+C中所做的
private string SearchDLScript(string searchDL)
{
listViewSearchDL.Items.Clear();
listViewSearchDL.Columns.Clear();
listViewSearchDL.Columns.Add(“”);
listViewSearchDL.Items.Add(“正在加载列表,请稍候”);
listViewSearchDL.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
if(textSearchDL.Text.Length<8)
{
添加(“提示:键入越多,搜索越快”);
listViewSearchDL.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
字符串rbName=“”;
如果(radioButtonDisplayName.Checked==true)
{
rbName=“DisplayName”;
}else if(radioButtonAlias.Checked==true)
{
rbName=“SamAccountName”;
}
字符串searchDLScriptCommand=@“导入模块ActiveDirectory
获取ADGroup-Filter'+rbName+@“-Like”“+searchDL+@”*“”“-Properties*|选择对象显示名称,SamAccountName |转换为Csv-NoTypeInformation |选择对象-跳过1”;
string scriptOutput=RunPowerShellCommands.RunPowerShellCode(searchDLScriptCommand);
string[]strArr=scriptOutput.Split(新字符串[]{System.Environment.NewLine},StringSplitOptions.None);
strArr=strArr.Where(x=>!string.IsNullOrEmpty(x)).ToArray();
listViewSearchDL.Columns.Clear();
listViewSearchDL.Items.Clear();
listViewSearchDL.Columns.Add(“显示名称”);
listViewSearchDL.Columns.Add(“别名”);
foreach(strArr中的字符串用户)
{
字符串userDetails=user.Replace(“\”,“”);
string[]columns=userDetails.Split(',');
ListViewItem lvi=新的ListViewItem(列[0]);
对于(int i=1;i
在C#示例中,通过调用GetDirectoryEntry()
,对DirectorySearcher返回的每个对象隐式执行两次额外的查找:
甚至警告您:
注意对通过DirectorySearcher返回的每个搜索结果调用GetDirectoryEntry可能会很慢
您要做的是将需要的属性名称列表添加到搜索程序中(这是
Get AD*-Properties
参数在后台执行的操作),并在第一次搜索后返回它们:
DirectorySearcher ds = new DirectorySearcher(de);
// do this before calling FindAll()
ds.PropertiesToLoad.Add("samaccountname")
然后在处理搜索结果时,直接从每个搜索结果中获取属性值,而不是再次调用GetDirectoryEntry()
:
foreach (SearchResult searchresult in rsAll)
{
if (searchresult.Properties["samaccountname"].Value != null)
{
samsaccountname = searchresult.Properties["samaccountname"].Value.ToString();
}
else
{
samsaccountname = "";
}
// and then updating the listview
}
您好,Mathias,谢谢您的帮助,但是将代码更改为上面的代码(不带GetDirectoryEntry())会导致错误“ResultPropertyValueCollection”不包含“Value”的定义……代码中是否缺少某些内容?
DirectorySearcher ds = new DirectorySearcher(de);
// do this before calling FindAll()
ds.PropertiesToLoad.Add("samaccountname")
foreach (SearchResult searchresult in rsAll)
{
if (searchresult.Properties["samaccountname"].Value != null)
{
samsaccountname = searchresult.Properties["samaccountname"].Value.ToString();
}
else
{
samsaccountname = "";
}
// and then updating the listview
}