Powershell 2.0为本地组获取用户

时间:2014-04-14 21:57:38

标签: powershell-v2.0

如何从本地组获取用户列表?我只有PS 2.0,它没有Get-ADGroup命令。

我可以找到当地团体:

$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
$groups = $adsi.Children | Where { $_.SchemaClassName -eq 'Group' }
$group | ft Name

我需要的是列出每个小组的所有成员。

2 个答案:

答案 0 :(得分:1)

您可以尝试以下

$obj = [ADSI]"WinNT://$env:COMPUTERNAME"
$admingroup = $obj.Children | Where { $_.SchemaClassName -eq 'group'} |  where {$_.name -eq 'Admitrateurs'}
$admingroup.Invoke('Members') | % {$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)}
$admingroup.Invoke('Members') | % {$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)}

以下是常见属性

字符串

Description, FullName, HomeDirectory, HomeDirDrive, Profile, LoginScript, ObjectSID

整数

UserFlags, PasswordExpired, PrimaryGroupID

时间

PasswordAge

您可以在Microsoft documentation中找到更多内容。

答案 1 :(得分:0)

试试这个

$computer = [ADSI]"WinNT://$env:COMPUTERNAME"

$computer.psbase.children | where { $_.psbase.schemaClassName -eq 'group' } | foreach {
    write-host $_.name
    write-host "------"
    $group =[ADSI]$_.psbase.Path
    $group.psbase.Invoke("Members") | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
    write-host
}

这不会给域名,因此我不得不寻找其他方式,例如:

如果您想快速查看本地组的成员:

PS C:\> net localgroup USERS
Alias name     USERS
Comment        Users are prevented from making accidental or intentional system-wide changes and can run most applications

Members

-------------------------------------------------------------------------------
NT AUTHORITY\Authenticated Users
NT AUTHORITY\INTERACTIVE
The command completed successfully.

现在您可以稍微操作此输出以获得所需内容:

$computer = [ADSI]"WinNT://$env:COMPUTERNAME"

$groups = $computer.psbase.children | where { $_.psbase.schemaClassName -eq 'group' } | select -ExpandProperty Name

Foreach($group in $groups)
 {
  write-host $group
  write-host "------"
  net localgroup $group | where {$_ -notmatch "command completed successfully"} | select -skip 6
  Write-host
 }