用户和本地组使用Powershell报告?

11 windows security powershell

有一种简单的方法使用PowerShell来显示在计算机上处​​于活动状态的所有本地Windows组以及属于这些组的用户吗?这个问题的第二部分是如果它可以扩展到一次查看多台机器.

Sha*_*evy 13

实际上你可以使用ADSI类型的快捷方式和WinNT名字对象.这是一个列出自己机器中的组和成员的示例:

$server="."
$computer = [ADSI]"WinNT://$server,computer"

$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
}
Run Code Online (Sandbox Code Playgroud)


Jar*_*Par 7

Powershell对此类功能没有任何固有的支持.但是,使用几个powershell函数包装"net localgroup"命令很容易,因此可以在管道中启用它.

获取本地组

function Get-LocalGroups() {
  net localgroup | ?{ $_ -match "^\*.*" } | %{ $_.SubString(1) };
}
Run Code Online (Sandbox Code Playgroud)

获取本地组成员

function Get-LocalGroupMembers() {
  param ([string]$groupName = $(throw "Need a name") )
  $lines = net localgroup $groupName
  $found = $false
  for ($i = 0; $i -lt $lines.Length; $i++ ) {
    if ( $found ) {
      if ( -not $lines[$i].StartsWith("The command completed")) {
        $lines[$i]
      }
    } elseif ( $lines[$i] -match "^----" ) {
      $found = $true;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)