• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1514
  • Last Modified:

Running query on AD

I'm trying to run this function/script on PSH; but I get the following error message.

Unexpected token '}' in expression or statement.
At C:\scripts\Dynamic_DN.ps1:34 char:2
+} <<<<
      + CategoryInfo            :  ParserError: (}:String) [ ], ParseException
      + FullyQualifiedErrorId      : UnexpectedToken

---
I function generates the DN dynamically
See script below:


function GetUserDN([string]$username)
{

$objADSI = [adsi]""
$domain = $objADSI.distinguishedname
$objDomain = [adsi]("LDAP://" + $domain)

$search = New-Object System.DirectoryServices.DirectorySearcher
$search.SearchRoot = $objDomain
$search.Filter = "(sAMAccountName=$username)"
$search.SearchScope = "Subtree"

$result = $search.FindOne()
if($result -eq $null)
{
return $null
} else {
return $result.GetDirectoryEntry().distinguishedName
     }
}

$username="administrator"
$userDN=GetUserDN($username)
if($userDN -eq $null)
{
Write-Host ("Unable to find " + $username)
} else {
Write-Host ($username + " - " + $userDN)
     }
}
0
PwrShell
Asked:
PwrShell
3 Solutions
 
Brent ChallisPrincipal: ITCommented:
Rather than using the ADSI work directory, can you use the Microsoft ActiveDirectory Module and associated cmdlets?  These require a 2008 R2 DC or the gateway services installed on to a pre 2008 R2 DC.  Then you can use cmdlets such as Get-ADUser.
0
 
Neil RussellTechnical Development LeadCommented:
Better still get the Quest QAD Commandlets for powershell. You whole code then becomes..

$UsersDN = (Get-qaduser -SamAccountName "$Username").DN
0
 
Neil RussellTechnical Development LeadCommented:
But in terms of YOUR ACTUAL code, you have an extra } here.....

if($result -eq $null)
{
    return $null
} else {
    return $result.GetDirectoryEntry().distinguishedName
}
} <--------- DELETE THIS ONE

$username="administrator"

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
X_layerCommented:
In you script delete the last }.
0
 
Neil RussellTechnical Development LeadCommented:
Yes well spotted! I said the wrong one, its the very very last one you need to delete!
0
 
PwrShellAuthor Commented:
Thank you. I'm just learning PSH so even though the code made sense after reading the book I guess I'm still adding or missing extra brackets. This Removing the bracket solved the problem.
Also, I will download and start using the Quest Tools. Never heard of them; but if these tools can compress the entire code to that one line, that is great. Thank you again.
0
 
PwrShellAuthor Commented:
Thank you for following up on this. I wanted to give points to 3 different users who helped with the question. One of them answered the question; but missed the wrong } symbol, so i awarded the correct answer to the person who got the } correct. But I would give points to both. Also the person who suggested using the Quest clmlets deserves points for suggesting tools that are great time savers and efficient at solving future queries/problems that may arise when working with PSH.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now