Harmond
Harmond

Reputation: 125

Powershell Active Directory

I am using PowerShell for the first time. I am trying to use a script that lets me get some attributes from Active Directory of all the people in a group. Below is a script that I found and tried using but it gave me an error. \

my OU.csv has content:

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

UserInfo.txt is empty

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

Upvotes: 0

Views: 476

Answers (1)

Mike Christensen
Mike Christensen

Reputation: 91726

It appears the ForEach-Object is not terminated. 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

Upvotes: 1

Related Questions