簡體   English   中英

Powershell Active Directory

[英]Powershell Active Directory

我是第一次使用PowerShell。 我正在嘗試使用一個腳本,該腳本使我可以從Active Directory中獲取組中所有人員的一些屬性。 以下是我找到並嘗試使用的腳本,但它給了我一個錯誤。 \\

我的OU.csv具有以下內容:

Dn“ OU =某物,OU =某物1,DC =某物2,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

似乎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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM