Powershell Active Directory

时间:2013-05-03 16:10:09

标签: 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 个答案:

答案 0 :(得分: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