-Filter“LocalAccount ='$ True'放置

时间:2013-08-24 12:43:05

标签: powershell foreach get admin

我在这个电源shell脚本中遇到问题,让脚本可以查看系统上的本地帐户。正如所写,它为每台计算机调查AD,这会对AD造成太大的压力。我相信我需要为Get-Wmiobject添加-Filter“LocalAccount ='$ True',但似乎无法将它放在正确的位置。任何帮助或建议都表示赞赏。此外,我认为这个脚本实际上是在做两次工作产生csv输出,有什么建议吗?

$Computers = Get-QADComputer -SizeLimit 0 | select -Expand name
    }


function Get-Admins {

Foreach ($Computer in $Computers){
$Online = Test-Connection -ComputerName $Computer -Quiet
    if ($Online -eq "True"){
    $GroupName = Get-WmiObject win32_group -ComputerName  $Computer | ? {$_.SID -eq 'S-     1-5-32-544'} | Select-Object name -ExpandProperty name
    $LocalGroup =[ADSI]"WinNT://$Computer/$GroupName"
    $GroupMembers = @($LocalGroup.psbase.Invoke("Members"))
    $Members = $GroupMembers | foreach {$_.GetType().InvokeMember("Name",   'GetProperty', $null, $_, $null)}
    foreach ($Member in $Members){
    $obj = New-Object System.Object
    $obj | Add-Member -MemberType NoteProperty -Name "Computer" -Value $Computer
    $obj | Add-Member -MemberType NoteProperty -Name "Local Admin" -Value $Member
    $obj
        }
    }
}
 }


Get-Admins | group { $_.Computer } | ForEach-Object {
  $computer = @($_.Group | Select-Object -Expand Computer)[0]
  $admins   = ($_.Group | Select-Object -Expand "Local Admin") -join ','
   '{0},{1}' -f $computer, $admins
 } | Out-File 'c:\scripts\CSV_Admin_export.csv'

2 个答案:

答案 0 :(得分:0)

我相信你可以通过改变Get-WmiObject调用来简化它:

Get-WmiObject Win32_Group -Filter "LocalAccount = $true AND SID = 'S-1-5-32-544'"

正如您所看到的,不仅仅使用Where-Object过滤掉您关心的SID,我也使用了WMI过滤器。

对于您要求的过滤器,以下任何一种都可以使用:

    "LocalAccount = 1"
    "LocalAccount = TRUE"
    "LocalAccount = 'TRUE'"
    "LocalAccount = $true"
    "LocalAccount = '$true'"

答案 1 :(得分:0)

使用过滤器获取目标计算机上的Administrators组:

$GroupName = (Get-WmiObject Win32_Group -Filter "Domain='$Computer' AND SID='S-1-5-32-544'" -ComputerName $Computer).Name