简体   繁体   English

Powershell Active Directory

[英]Powershell Active Directory

I am using PowerShell for the first time. 我是第一次使用PowerShell。 I am trying to use a script that lets me get some attributes from Active Directory of all the people in a group. 我正在尝试使用一个脚本,该脚本使我可以从Active Directory中获取组中所有人员的一些属性。 Below is a script that I found and tried using but it gave me an error. 以下是我找到并尝试使用的脚本,但它给了我一个错误。 \\ \\

my OU.csv has content: 我的OU.csv具有以下内容:

Dn "OU=something,OU=something1,DC=something2,DC=com" Dn“ OU =某物,OU =某物1,DC =某物2,DC = com”

UserInfo.txt is empty UserInfo.txt为空

SearchAD_UserInfo: 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 
   } 

Error: 错误:

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

It appears the ForEach-Object is not terminated. 似乎ForEach-Object没有终止。 Change: 更改:

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 
   } 

To: 至:

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