使用VBS中通过WMI获取AD中邮件组成员

WMI是Windows Management Instrumentation,Windows自带的框架。Windows本身提供了两个WMI工具供用户使用:WMIC.exe、WBEMTest.exe。除了这两个还可以下载WMI Administrative Tools。


通过WMI可以获取AD邮件组中的所有用户,刚写了个方法还挺好用,VBS的直接运行就行

Function getAllUserInDomainGroup(domain, group)
	counter = 0
	Set objGroups = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("Select * from Win32_GroupUser Where GroupComponent=""Win32_Group.Domain='" & domain & "',Name='" & group & "'""")


	For Each group In objGroups
		uIndex = InStr(group.PartComponent,"Name=""")
		uName=Mid(group.PartComponent, uIndex+6, Len(group.PartComponent)-uIndex-6)
		msgbox uName
		counter = counter + 1
	Next
	getAllUserInDomainGroup = counter
End Function

用C#取AD组中的用户还有更方便的方法:

PrincipalContext context = new PrincipalContext(ContextType.Domain);
GroupPrincipal group = new GroupPrincipal(context, item);
PrincipalSearcher search = new PrincipalSearcher(group);
GroupPrincipal foundGroup = search.FindOne() as GroupPrincipal;
foreach (Principal princ in foundGroup.Members)
{
	if (princ.StructuralObjectClass == "user")
	{
		members.Add(princ.SamAccountName.ToString());
	}
	if (princ.StructuralObjectClass == "group")
	{
		InitMems(princ.SamAccountName, members, tempGroup);
	}
}

看需要可以调用即可。



展开阅读全文

没有更多推荐了,返回首页