0

我正在尝试使用 try 和 catch 方法,因此当主机不适合而不是跳过它时,脚本将显示。这没有按预期工作,有什么想法吗?

#Gets list of machines from specified OU
Function Get-CompList{
Get-ADObject -Filter { ObjectClass -eq "computer" } -SearchBase "OU=Resources,DC=NWTraders,DC=LOCAL" `
| Select-Object -expandproperty Name
}

#Gets Admin accounts from computer in OU from Get-Complist function
Function Get-AdminGroups{

foreach($i in Get-CompList){
Try{

$adsi = [ADSI]"WinNT://$i"
$Object = $adsi.Children | ? {$_.SchemaClassName -eq 'user'} | % {
    New-Object -TypeName PSCustomObject -Property @{
        ComputerName = $i -join ''
        UserName = $_.Name -join ''
        Groups = ($_.Groups()  |Foreach-Object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}) -join ',' 
          }  
      } 


   $Object |? {$_.Groups -match "Administrators*"}  
   "`r"
  }

Catch{
"$i is unavalible"
"`r"
     }
  }
}

Get-AdminGroups
4

0 回答 0