【问题标题】:Powershell Active DirectoryPowershell 活动目录
【发布时间】:2013-05-04 00:14:43
【问题描述】:

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

我的 OU.csv 有内容:

Dn "OU=something,OU=something1,DC=something2,DC=com"

UserInfo.txt 为空

搜索AD_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

【问题讨论】:

    标签: powershell active-directory


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      相关资源
      最近更新 更多