Windows PowerShell 2 命令捕获组中的本地用户帐户
Windows PowerShell 2 Command To Capture Local User Accounts In A Group
我仅限于 PowerShell 2,并且我一直在尝试制作一个命令来列出某个组(例如管理员)中的本地用户帐户。然而,精心制作的命令还列出了 DC 用户帐户,我不想要这个,有没有办法实现这个?
精心制作的命令:
gwmi win32_group -filter 'Name="Administrators"'|%{$_.GetRelated('Win32_UserAccount')} | select Name
假设您可以访问 [adsi]
类型加速器,这应该为您提供本地 Administrators:
的成员
$adsi=[adsi]"WinNT://$env:ComputerName,computer"
$adsi.psbase.children.find('Administrators').psbase.invoke('members')|
%{
$name=$_.GetType().InvokeMember('Name','GetProperty',$null,$_,$null)
$class=$_.GetType().InvokeMember('Class','GetProperty',$null,$_,$null)
$adspath=$_.GetType().InvokeMember('ADSPath','GetProperty',$null,$_,$null)
$sid=New-Object System.Security.Principal.SecurityIdentifier -ArgumentList `
($_.GetType().invokeMember('objectsid','GetProperty',$null,$_,$null),0)
[pscustomobject]@{
ComputerName=$env:ComputerName
Name=$name
Class=$class
Path=$adspath -replace 'WinNT://'
SecurityIdentifier=$sid.Value
}
}|sort Class -Descending
一行:
$adsi=[adsi]"WinNT://$env:ComputerName,computer";$adsi.psbase.children.find('Administrators').psbase.invoke('members')|%{$name=$_.GetType().InvokeMember('Name','GetProperty',$null,$_,$null);$class=$_.GetType().InvokeMember('Class','GetProperty',$null,$_,$null);$adspath=$_.GetType().InvokeMember('ADSPath','GetProperty',$null,$_,$null);$sid=New-Object System.Security.Principal.SecurityIdentifier -ArgumentList ($_.GetType().invokeMember('objectsid','GetProperty',$null,$_,$null),0);[pscustomobject]@{ComputerName=$env:ComputerName;Name=$name;Class=$class;Path=$adspath -replace 'WinNT://';SecurityIdentifier=$sid.Value}}|sort Class -Descending
为什么不直接使用 PowerShell 的内置 OS 工具来解析输出?
# Get all group names
net localgroup
# Get members of one group
net localgroup administrators
(net localgroup administrators) -replace 'The command completed successfully.|\-+' | Select-Object -Skip 4
# Results
<#
Administrator
...
#>
即使是 PowerShell,老派仍然是一回事。
就您的 PowerShell v2(不再支持、不必要的风险问题等)而言。首先,真的(如果你有影响力的话)你需要说服他们放弃。
;-}
但是,对于 v2,请按此方式执行命令。
坦白说,我已经很多年没有使用 v2 了,所以,必须在一个 Win10 系统上重新启用它才能做到这一点。
powershell -version 2.0 -nologo -noprofile
$PSVersionTable
# Results
<#
Name Value
---- -----
CLRVersion 2.0.50727.9151
BuildVersion 6.1.7600.16385
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
#>
Get-wmiobject -Class Win32_OperatingSystem
# Results
<#
SystemDirectory : C:\WINDOWS\system32
Organization :
BuildNumber : 19042
RegisteredUser : User001
SerialNumber : 00330...
Version : 10.0.19042
#>
Get-WmiObject win32_group -filter 'Name="Administrators"'
# Results
<#
Caption Domain Name SID
------- ------ ---- ---
w10labws001\Administrators w10labws001 Administrators S-1-5-...
#>
(Get-WmiObject win32_group -filter 'Name="Administrators"').GetRelated('Win32_UserAccount')
# Results
<#
AccountType : 512
Caption : w10labws001\Administrator
Domain : w10labws001
SID : S-1-5-21...
FullName :
Name : Administrator
AccountType : 512
Caption : w10labws001\User001
Domain : w10labws001
SID : S-1-5-21-...
FullName :
Name : User001
...
#>
或
((Get-WmiObject win32_group -filter 'Name="Administrators"').GetRelated('Win32_UserAccount')).Name
# Results
<#
Administrator
...
#>
上面的命令和你原来的命令一样有效。
C:\>powershell -version 2.0 -nologo -noprofile
PS C:\> gwmi win32_group -filter 'Name="Administrators"'|%{$_.GetRelated('Win32_UserAccount')} | select Name
# Results
<#
Name
----
Administrator
...
#>
完成工作所需的时间比 net localgroup
和/或Santiago Squarzon
向您展示的方式。
我仅限于 PowerShell 2,并且我一直在尝试制作一个命令来列出某个组(例如管理员)中的本地用户帐户。然而,精心制作的命令还列出了 DC 用户帐户,我不想要这个,有没有办法实现这个?
精心制作的命令:
gwmi win32_group -filter 'Name="Administrators"'|%{$_.GetRelated('Win32_UserAccount')} | select Name
假设您可以访问 [adsi]
类型加速器,这应该为您提供本地 Administrators:
$adsi=[adsi]"WinNT://$env:ComputerName,computer"
$adsi.psbase.children.find('Administrators').psbase.invoke('members')|
%{
$name=$_.GetType().InvokeMember('Name','GetProperty',$null,$_,$null)
$class=$_.GetType().InvokeMember('Class','GetProperty',$null,$_,$null)
$adspath=$_.GetType().InvokeMember('ADSPath','GetProperty',$null,$_,$null)
$sid=New-Object System.Security.Principal.SecurityIdentifier -ArgumentList `
($_.GetType().invokeMember('objectsid','GetProperty',$null,$_,$null),0)
[pscustomobject]@{
ComputerName=$env:ComputerName
Name=$name
Class=$class
Path=$adspath -replace 'WinNT://'
SecurityIdentifier=$sid.Value
}
}|sort Class -Descending
一行:
$adsi=[adsi]"WinNT://$env:ComputerName,computer";$adsi.psbase.children.find('Administrators').psbase.invoke('members')|%{$name=$_.GetType().InvokeMember('Name','GetProperty',$null,$_,$null);$class=$_.GetType().InvokeMember('Class','GetProperty',$null,$_,$null);$adspath=$_.GetType().InvokeMember('ADSPath','GetProperty',$null,$_,$null);$sid=New-Object System.Security.Principal.SecurityIdentifier -ArgumentList ($_.GetType().invokeMember('objectsid','GetProperty',$null,$_,$null),0);[pscustomobject]@{ComputerName=$env:ComputerName;Name=$name;Class=$class;Path=$adspath -replace 'WinNT://';SecurityIdentifier=$sid.Value}}|sort Class -Descending
为什么不直接使用 PowerShell 的内置 OS 工具来解析输出?
# Get all group names
net localgroup
# Get members of one group
net localgroup administrators
(net localgroup administrators) -replace 'The command completed successfully.|\-+' | Select-Object -Skip 4
# Results
<#
Administrator
...
#>
即使是 PowerShell,老派仍然是一回事。
就您的 PowerShell v2(不再支持、不必要的风险问题等)而言。首先,真的(如果你有影响力的话)你需要说服他们放弃。
;-}
但是,对于 v2,请按此方式执行命令。
坦白说,我已经很多年没有使用 v2 了,所以,必须在一个 Win10 系统上重新启用它才能做到这一点。
powershell -version 2.0 -nologo -noprofile
$PSVersionTable
# Results
<#
Name Value
---- -----
CLRVersion 2.0.50727.9151
BuildVersion 6.1.7600.16385
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
#>
Get-wmiobject -Class Win32_OperatingSystem
# Results
<#
SystemDirectory : C:\WINDOWS\system32
Organization :
BuildNumber : 19042
RegisteredUser : User001
SerialNumber : 00330...
Version : 10.0.19042
#>
Get-WmiObject win32_group -filter 'Name="Administrators"'
# Results
<#
Caption Domain Name SID
------- ------ ---- ---
w10labws001\Administrators w10labws001 Administrators S-1-5-...
#>
(Get-WmiObject win32_group -filter 'Name="Administrators"').GetRelated('Win32_UserAccount')
# Results
<#
AccountType : 512
Caption : w10labws001\Administrator
Domain : w10labws001
SID : S-1-5-21...
FullName :
Name : Administrator
AccountType : 512
Caption : w10labws001\User001
Domain : w10labws001
SID : S-1-5-21-...
FullName :
Name : User001
...
#>
或
((Get-WmiObject win32_group -filter 'Name="Administrators"').GetRelated('Win32_UserAccount')).Name
# Results
<#
Administrator
...
#>
上面的命令和你原来的命令一样有效。
C:\>powershell -version 2.0 -nologo -noprofile
PS C:\> gwmi win32_group -filter 'Name="Administrators"'|%{$_.GetRelated('Win32_UserAccount')} | select Name
# Results
<#
Name
----
Administrator
...
#>
完成工作所需的时间比 net localgroup
和/或Santiago Squarzon
向您展示的方式。