我在这个 power 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'