本地管理员报告未显示域组

Local Admins report not showing domain groups

我正在尝试使用以下脚本将一组服务器上的所有本地管理员成员和域组导出到 csv 文件中。导出本地管理员工作正常,但我注意到它不导出域组(即:我在本地管理员组中有一个域管理员组,但它没有显示在 csv 中)。

这是我的代码,如有任何帮助,我们将不胜感激:

param(
  [parameter(Position=0,ValueFromPipeline=$true)]
    $ComputerName=[Net.Dns]::GetHostName(),
    [System.Management.Automation.PSCredential] $Credential,
    [UInt32] $BlockSize=50
)

begin {
  $WMIEnumOpts = new-object System.Management.EnumerationOptions
  $WMIEnumOpts.BlockSize = $BlockSize

  function Get-LocalAdminGroupMember {
    param(
      [String] $computerName,
      [System.Management.Automation.PSCredential] $credential
    )
    $params = @{
      "Class" = "Win32_Group"
      "ComputerName" = $computerName
      "Filter" = "LocalAccount=TRUE and SID='S-1-5-32-544'"
    }
    if ( $credential ) {
      if ( $computerName -eq [Net.Dns]::GetHostName() ) {
        Write-Warning "The -Credential parameter is ignored for the current computer."
      }
      else {
        $params.Add("Credential", $credential)
      }
    }
    Get-WmiObject @params | ForEach-Object {
      $groupName = $_.Name
      $_.GetRelated("Win32_Account","Win32_GroupUser","","",
        "PartComponent","GroupComponent",$false,$WMIEnumOpts) | Select-Object `
          @{Name="ComputerName"; Expression={$_.__SERVER}},
          @{Name="Name"; Expression={$groupName}},
          @{Name="Member"; Expression={$_.Caption -replace "^$($_.__SERVER)\", ""}},
          @{Name="Type"; Expression={$_.__CLASS}}
    }
  }
}
process {
$Filename = PATH HERE
$OutFileName = "C:\temp\admins.csv"

Get-Content $Filename | Foreach-Object {Get-LocalAdminGroupMember -computerName $_ | Select-Object * | Export-csv -NoType $OutFileName -Append}

啊,尝试从远程计算机访问网络资源的乐趣。你将失去任何域帐户做你正在做的事情。这就是它的工作原理。好消息是,仍然有办法获取您想要的信息,如果您愿意,甚至可以使用 Get-WmiObject 来获取。如果您没有重命名 Administrators 组(因为真的,是谁做的?),您可以轻松地做到这一点,但如果您这样做了并且您必须像上面那样按 SID 查找组,那么您将不得不查询像您一样的远程服务器,并使用您返回的修改后的名称对下面的查询进行调整。这是我推荐的做法,使用 Win32_GroupUser class 代替:

Get-WmiObject -ComputerName $Server -Query "SELECT * FROM win32_GroupUser WHERE GroupComponent = ""Win32_Group.Domain='$computerName',Name='Administrators'"""

要用它代替您的函数,它可能看起来像这样:

function Get-LocalAdminGroupMember {
    param(
        [String] $computerName,
        [System.Management.Automation.PSCredential] $credential
    )
    $params = @{
        "ComputerName" = $computerName
        "Query" = "SELECT * FROM win32_GroupUser WHERE GroupComponent = ""Win32_Group.Domain='$computerName',Name='Administrators'"""
    }
    if ( $credential ) {
        if ( $computerName -eq [Net.Dns]::GetHostName() ) {
        Write-Warning "The -Credential parameter is ignored for the current computer."
        }
        else {
        $params.Add("Credential", $credential)
        }
    }
    Get-WmiObject @params | 
        Where{$_.PartComponent -match ':(.+?)\.Domain="(.+?)",Name="(.+?)"'}|
        ForEach{
            [PSCustomObject]@{
                "ComputerName"=$computerName
                "Name"='Administrators'
                "Member"=$Matches[2..3] -join '\' -replace "^$computerName\"
                "Type"=$Matches[1]
            }
        }
}