尝试Catch输出

时间:2013-11-13 20:03:12

标签: powershell try-catch

我正在尝试使用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

0 个答案:

没有答案