2013-05-03 152 views
0

我第一次使用PowerShell。我正在尝试使用一个脚本,可以让我从Active Directory中获取组中所有人的一些属性。下面是我找到并尝试使用的脚本,但它给了我一个错误。 \Powershell Active Directory

我OU.csv有内容:

DN “OU =东西,OU = something1,DC = something2,DC = COM”

UserInfo.txt是空

SearchAD_UserInfo:

# Search Active Directory and Get User Information 
# 
# www.sivarajan.com 
# 

clear 
$UserInfoFile = New-Item -type file -force "C:\Scripts\UserInfo.txt" 
"samaccountname`tgivenname`tSN" | Out-File $UserInfoFile -encoding ASCII 
Import-CSV "C:\Scripts\OU.csv" | ForEach-Object { 
    $dn = $_.dn 
    $ObjFilter = "(&(objectCategory=User)(objectCategory=Person))" 
    $objSearch = New-Object System.DirectoryServices.DirectorySearcher 
    $objSearch.PageSize = 15000 
    $objSearch.Filter = $ObjFilter 
    $objSearch.SearchRoot = "LDAP://$dn" 
    $AllObj = $objSearch.FindAll() 
    foreach ($Obj in $AllObj) 
     { $objItemS = $Obj.Properties 
      $Ssamaccountname = $objItemS.samaccountname 
      $SsamaccountnameGN = $objItemS.givenname 
      $SsamaccountnameSN = $objItemS.sn 
      "$Ssamaccountname`t$SsamaccountnameGN`t$SsamaccountnameSN" | Out-File $UserInfoFile -encoding ASCII -append 
    } 

错误:

Missing closing '}' in statement block. 
At C:\Path\SearchAD_UserInfo 
+ } <<<< 
    + CategoryInfo   : ParserError: (CloseBra 
    + FullyQualifiedErrorId : MissingEndCurlyBrace 

回答

1

它出现ForEach-Object不被终止。变化:

foreach ($Obj in $AllObj) 
     { $objItemS = $Obj.Properties 
      $Ssamaccountname = $objItemS.samaccountname 
      $SsamaccountnameGN = $objItemS.givenname 
      $SsamaccountnameSN = $objItemS.sn 
      "$Ssamaccountname`t$SsamaccountnameGN`t$SsamaccountnameSN" | Out-File $UserInfoFile -encoding ASCII -append 
    } 

要:

foreach ($Obj in $AllObj) 
     { $objItemS = $Obj.Properties 
      $Ssamaccountname = $objItemS.samaccountname 
      $SsamaccountnameGN = $objItemS.givenname 
      $SsamaccountnameSN = $objItemS.sn 
      "$Ssamaccountname`t$SsamaccountnameGN`t$SsamaccountnameSN" | Out-File $UserInfoFile -encoding ASCII -append 
     } # End of foreach 
    } # End of ForEach-Object 
+0

太感谢你了!这工作! – Harmond 2013-05-03 17:31:37