2

I apologize in advance if this is something simple, but I've been researching for a few days now and unable to resolve on my own or find another route to explore.

Essentially I have the below which works and returns exactly what I need:

([adsisearcher]'(&(objectClass=user (samaccountname='*dland*'))').FindOne().Properties['samaccountname']    

which returns the username containing dland in it. I want to replace the dland with a variable ($usrNme1), but it errors out with "Unexpected token '$usrNme1'))'' in expression or statement."

I was able to get it working locally with using the ActiveDirectory module using the following:

Get-ADUser -Filter {SAMAccountName -like $usrNme1} | select-object -Property SAMAccountName    

But unfortunately I cannot use that against other computers in this environment and have to find another way and this is as far as I've gotten to finding a replacement.

Any assistance here would be greatly appreciated :) This is the last piece of the puzzle for me and its frustrating being so close and not being able to figure it out! Thanks in advance for taking the time :)

Edit: Forgot to comment, this script is going to be pushed out and run locally on windows 7 machines, which is part of the reason why I can't use Get-ADUser.

Thanks,

David

2 Answers2

1

Your query is a little malformed as it is missing a bracket after user but you can put variables in the string easily like in the following example. Variables placed inside double quotes will expand just fine* (Most of the time. Object parameters require subexpressions).

$accountname = "mcame*"
$query = "(&(objectClass=user)(samaccountname=$accountname))"
([adsisearcher]$query).FindOne().Properties['samaccountname']

Note: if you look at this question you will see issues doing the wildcard search that your are. If you have a large organization you might need to reconsider using leading and trailing asterices or whatever the plural is.

You original query

Aside from the bracket the reason it was not working was since you were using the single quotes. If you look at this resource it goes on to say

Comparative strings do NOT appear in quotation marks. A filter for the displayName 'Philipp Foeckeler' would read as follows: (displayName=Philipp Foeckeler).

Query should have worked without those inner quotes.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Hi Matt, thanks that worked quite well. The query I was originally working with had the ) after user, it still didn't work. Is it because I was trying to one line it? I guess what throws me off is that it worked without the variable using dland* but failed once I introduced it (original error is "Cannot index into a null array." Just trying to understand the difference here so I don't get caught by it again... – David Venthe Dec 23 '14 at 18:08
  • @DavidVenthe see updated answer that covers that question of yours. – Matt Dec 23 '14 at 18:12
0

Try this:

$foo = '*jsm*'
([adsisearcher]"(&(objectClass=user) (samaccountname=$foo))")
jbsmith
  • 1,616
  • 13
  • 10
  • What didn't work with this? Was there an error? I tested it and this method works. – jbsmith Dec 23 '14 at 18:49
  • This returned all account properties rather than just the account name, which wasn't what I was looking to do. I guess saying it "didn't work" wasn't appropriate, but rather not what I needed :) – David Venthe Dec 29 '14 at 12:10
  • Oh, in that case just enclose it in parenthesis and use the ".FindOne().Properties['samaccountname']" syntax you already had, or just pipe the results to Select-Object to get what you need. – jbsmith Dec 29 '14 at 14:43