如何加快查询 Win32_UserAccount 按 FullName 属性 过滤
How can I speed up a query to Win32_UserAccount filtering by FullName property
我正在尝试根据用户的 FullName
属性 在 powershell 中使用简单的 WMI 命令查找特定用户:
Get-WmiObject win32_useraccount -Filter "fullname='Jack Ryan'"
大约有 50,000 个用户,根据 FullName
属性.
查找所有用户最多需要 30 秒
然而,当我尝试基于 Name
而不是 FullName
进行搜索时,不到一秒钟我就得到了回复。我不明白为什么按全名查找要花这么长时间。
我怎样才能加快速度? (不幸的是,我需要根据全名进行过滤并获取这些身份的所有用户名)
根据@rboy 评论,通过 ADSI 获取群组成员。如果您不能使用 Get-LocalGroupMember
.
,可能最容易在这里拥有自己的功能
function Get-LocalMembers {
Param(
[parameter(Mandatory=$false)][string]$GroupName
)
$ADSI = [ADSI]"WinNT://$env:COMPUTERNAME"
if ($PSBoundParameters.ContainsKey('GroupName')) {
$Groups = $ADSI.Children.Find($GroupName,'Group')
}
else {
$Groups = $ADSI.Children | Where-Object { $_.SchemaClassName -eq 'Group' }
}
Foreach ($Group in $Groups) {
[PSCustomObject] @{
Group = $($Group | Select-Object -ExpandProperty Name)
Members = $(
$Group.Invoke('members') | ForEach-Object {
$_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null)
}
)
}
}
}
用法:
Get-LocalMembers # Get members of all groups
Get-LocalMembers -GroupName Administrators # Get members of specified group
编辑
获取名称和全名属性。我真的不确定我们可以在这里使用 PowerShell 中的 ADSI 做多少。除了用户对象和 group/member 列表之外,我从未真正探索过它。
function Get-LocalMembers {
Param(
[parameter(Mandatory=$false)][string]$GroupName
)
$ADSI = [ADSI]"WinNT://$env:COMPUTERNAME"
if ($PSBoundParameters.ContainsKey('GroupName')) {
$Groups = $ADSI.Children.Find($GroupName,'Group')
}
else {
$Groups = $ADSI.Children | Where-Object { $_.SchemaClassName -eq 'Group' }
}
Foreach ($Group in $Groups) {
[PSCustomObject] @{
Group = $($Group | Select-Object -ExpandProperty Name)
Members = $(
$Group.Invoke('members') | ForEach-Object {
[PSCustomObject] @{
Name = $_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null)
FullName = $(
# A Group can be a member of a group and doesn't have a 'FullName property'
Try {
$_.GetType().InvokeMember("Fullname",'GetProperty',$null,$_,$null)
}
Catch {
"Group"
}
)
}
}
)
}
}
}
Disclaimer: I'm running this on a local, non-domain joined laptop none of my accounts have the Fullname property populated.
我正在尝试根据用户的 FullName
属性 在 powershell 中使用简单的 WMI 命令查找特定用户:
Get-WmiObject win32_useraccount -Filter "fullname='Jack Ryan'"
大约有 50,000 个用户,根据 FullName
属性.
然而,当我尝试基于 Name
而不是 FullName
进行搜索时,不到一秒钟我就得到了回复。我不明白为什么按全名查找要花这么长时间。
我怎样才能加快速度? (不幸的是,我需要根据全名进行过滤并获取这些身份的所有用户名)
根据@rboy 评论,通过 ADSI 获取群组成员。如果您不能使用 Get-LocalGroupMember
.
function Get-LocalMembers {
Param(
[parameter(Mandatory=$false)][string]$GroupName
)
$ADSI = [ADSI]"WinNT://$env:COMPUTERNAME"
if ($PSBoundParameters.ContainsKey('GroupName')) {
$Groups = $ADSI.Children.Find($GroupName,'Group')
}
else {
$Groups = $ADSI.Children | Where-Object { $_.SchemaClassName -eq 'Group' }
}
Foreach ($Group in $Groups) {
[PSCustomObject] @{
Group = $($Group | Select-Object -ExpandProperty Name)
Members = $(
$Group.Invoke('members') | ForEach-Object {
$_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null)
}
)
}
}
}
用法:
Get-LocalMembers # Get members of all groups
Get-LocalMembers -GroupName Administrators # Get members of specified group
编辑
获取名称和全名属性。我真的不确定我们可以在这里使用 PowerShell 中的 ADSI 做多少。除了用户对象和 group/member 列表之外,我从未真正探索过它。
function Get-LocalMembers {
Param(
[parameter(Mandatory=$false)][string]$GroupName
)
$ADSI = [ADSI]"WinNT://$env:COMPUTERNAME"
if ($PSBoundParameters.ContainsKey('GroupName')) {
$Groups = $ADSI.Children.Find($GroupName,'Group')
}
else {
$Groups = $ADSI.Children | Where-Object { $_.SchemaClassName -eq 'Group' }
}
Foreach ($Group in $Groups) {
[PSCustomObject] @{
Group = $($Group | Select-Object -ExpandProperty Name)
Members = $(
$Group.Invoke('members') | ForEach-Object {
[PSCustomObject] @{
Name = $_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null)
FullName = $(
# A Group can be a member of a group and doesn't have a 'FullName property'
Try {
$_.GetType().InvokeMember("Fullname",'GetProperty',$null,$_,$null)
}
Catch {
"Group"
}
)
}
}
)
}
}
}
Disclaimer: I'm running this on a local, non-domain joined laptop none of my accounts have the Fullname property populated.