I am trying to get the AD user account via Powershell, I need to import the name from csv and retrieve their AD results.
The list is only stored with "Display Name"
test.csv
name
Peter Chan
John Wu
Tom Wong
PS script
$list = Import-Csv '.\test.csv'
foreach ($i in $list) {
Get-ADUser -Filter "Name -eq '$i.name'"
}
Error
Get-ADUser : Error parsing query: 'Name -eq #{name=Peter Chan}.name' Error Message: 'syntax error' at position: '10'.
At line:2 char:1
+ Get-ADUser -Filter "Name -eq $i.name"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : Error parsing query: 'Name -eq #{name=Peter Chan}.name' Error Message: 'syntax error' at
position: '10'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
When I run Get-ADUser -Filter "Name -like 'Peter Chan'" , I can get the result i want. It shows that it is a array type #{name=Peter Chan}.name, what should I need to change on the code?
Variables get expanded in strings not property expressions.
Change this:
Get-ADUser -Filter "Name -eq '$i.Name'"
TO
Get-ADUser -Filter "Name -eq '$($i.Name)'"
Related
Unable to get the AD users created in the past 1 year which is not contains the specific Domain name pattern:
$laterThan = (Get-Date).AddYears(-1)
$filter = { (whenCreated -gt $laterThan) -and (userPrincipalName -notcontain $((Get-ADDomain).Name)) }
$properties = 'canonicalName', 'whenCreated', 'displayName', 'lastlogondate'
Get-ADUser -Filter $filter -Properties $properties
Error showing as:
Get-ADUser : Error parsing query: ' (whenCreated -gt $laterThan) -and
(userPrincipalName -notcontain $((Get-ADDomain).Name)) ' Error
Message: 'Operator Not supported: -notcontain' at position: '55'. At
line:5 char:1
Get-ADUser -Filter $filter -Properties $properties
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
However, the IDE not showing any issue:
You probably mean -notcontains but that operator is not supported for that cmdlet. Look here.
Also, that operator works with collections, not strings. If you want to check if a string contains another string, use the -like operator and wildcards:
Get-ADUser -Filter "UserPrincipalName -like '*$((Get-ADDomain).Name)*'"
That's because the correct operator is -notcontains
Check them all here.
I am looking for all accounts that have “(FUR)” in the begging of their AD account description. I need these for any and all OUs under Office/Users. All of these accounts that Have “(FUR)” in the description, I need the following exported;
User Logon Name
Description
extensionattribute11
This is what I have come up with so far:
Import-Module ActiveDirectory;
$creds = Get-Credential
$OUPath = 'OU=Standard Users,OU=NY,OU=users,OU=Offices,DC=US,DC=FLN,DC=NET'
Get-ADUser -Properties Description -Filter "(FUR)" -SearchBase $OUPath
This is the error I get:
Get-ADUser : Error parsing query: '(FUR)' Error Message: 'syntax
error' at position: '5'. At line:2 char:1
+ Get-ADUser -Properties Description -Filter "(FUR)" -SearchBase $OUPat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
When using the -Filter parameter, you must pass a string that contains the syntax propertyName -operator value.
Get-ADUser -Properties Description -Filter "Description -like '(FUR)*'" -SearchBase $OUPath
(FUR)* would match a value that begins with (FUR). You would need to use *(FUR)* if you do not know where (FUR) exists within the value.
See Get-ADUser for a more in-depth description.
I'm trying to import a CSV file that will update users in our AD environment with their proper email addresses.
The first few lines of the CSV look like this:
Email,Name
adam.lambert#domain.com,Adam Lambert
aaron.smith#domain.com,Aaron Smith
arthur.aardvark#domain.com,Arthur Aardvark
The PowerShell script I've written looks like this:
Import-Module ActiveDirectory
$data = Import-CSV -Path "C:\Users\redacted\Desktop\importtest.csv"
Foreach ($user in $data){
Get-ADUser -Filter “Name -eq ‘$($user.Name)'” | Set-ADUser -Replace #{mail = “$($user.Email)”}
}
However, much to my dismay, it doesn't work. To clarify, I am trying to match the ADSI value of "name" (not the SamAccountName) with the list in Excel, which I know for a fact are exact matches.
Any thoughts? Error is below:
Get-ADUser : Error parsing query: 'Name -eq ‘Adam Lambert'' Error Message: 'syntax error' at position: '10'.
At line:4 char:11
+ Get-ADUser <<<< -Filter “Name -eq ‘$($user.Name)'” | Set-ADUser -Replace #{mail = “$($user.Email)”}
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : Error parsing query: 'Name -eq ‘Adam Lambert'' Error Message: 'syntax error' at position: '10'.
,Microsoft.ActiveDirectory.Management.Commands.GetADUser
I think you have a back tick in that string:
shouldn't it be:
"Name -eq '$($user.Name)'"
I am attempting to write some scripts to match a partial AD user account name into a get-aduser script to return the objects for another routine.
However, the command when executed it returning a parse error
The strange thing is that when checking the syntax of the output, it looks AOK:
PS C:\Users> $ADUserString = "dcro"
PS C:\Users> write-host get-aduser -filter "{SAMAccountName -like '"$ADUserString*'"}"
get-aduser -filter {SAMAccountName -like "dcro*"}
Note: I have used the right-tick character ` prepeding the quotes on the variable to keep them as a string value
So when executing the command:
PS C:\Users> get-aduser -filter "{SAMAccountName -like "$ADUserString*"}"
get-aduser : Error parsing query: '{SAMAccountName -like "dcro"}' Error Message: 'syntax error' at position: '1'.
At line:1 char:1
+ get-aduser -filter "{SAMAccountName -like "$ADUserString*"}"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : Error parsing query: '{SAMAccountName -like "dcro*"}' Error Message: 'syntax error' at position: '1'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser*
However, if I manually type the output from my 'write-host' above, it executes perfectly fine and returns the results I am after:
PS C:\Users> get-aduser -filter {samaccountname -like "dcro*"}
DistinguishedName : CN=Dan*****,OU=A*****port,OU=Development*****************
Enabled : True
GivenName : D****
Name : D*****Cro****
ObjectClass : user
ObjectGUID : 796b**********413-558d*****d73
SamAccountName : dcro*****
SID : S-1******************67
Surname : Cro******
UserPrincipalName : dcro***********
It's pretty odd, and my feeling is that there are some weird special characters at play here.....
Try this:
$ADUserString = "dcro*"
write-host (Get-ADUser -Filter {SamAccountName -like $ADUserString} | Out-String)
Workaround I found was this (not so pretty).
$ADUserString = "dcro"
$AdUserStringWildCard = "$AdUserString*"
Get-ADUser -Filter {SamAccountName -like $AdUserStringWildCard}
Have you tried LDAPFilter anr instead? I know it tends to be iffy but might work in your scenario. It seems good at completing usernames.
Get-ADUser -LDAPFilter "(anr=$ADUserString)"
I have CSV file:
phone username
-------- ---------
1 user1
2 user2
first I run:
$arr = import-csv "c:\names.csv"
so for example this is the output when I run:
$arr[0].username
user1
Now, my goal is to find this user in AD and when I run:
$user = get-aduser -filter {Name -eq $arr[0].username}
I received:
Get-ADUser : Error parsing query: 'Name -eq $arr[0].username' Error
Message: 'Operator Not supported: ' at position: '1 4'. At line:1
char:19
+ $user = get-aduser <<<< -filter {Name -eq $arr[0].username}
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : Error parsing query: 'Name -eq $arr[0].username' Error Message: 'Operator Not supported:
' at position: '14'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
But, when I run:
$user = get-aduser -filter {Name -eq "user1"}
it works fine.
how can I find the users using $arr[0].username ?
what is the difference ? both of them strings:
"user1" and $arr[0].username ...
Thanks,
for your help
You can assign the username to a variable and use it in the query:
$name = $arr[0].username
$user = get-aduser -filter {Name -eq $name}
or use an expanded string instead of a script block:
$user = get-aduser -filter "Name -eq '$($arr[0].username)'"