2013-05-03 126 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