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)'"
Related
Trying to search AD account properties pulling from a CSV. The Import-CSV line works by itself. I cannot for the life of me figure out why it is asking for a filter. I took this from another script I found where they said it worked. Others were using a For-Each statement.
PS C:\Users\XXXXX> Import-CSV .\listofnames.csv | Get-ADUser $_.DisplayName -properties displayname
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument
collection contains a null value.
At line:1 char:43
+ Import-CSV .\listofnames.csv | Get-ADUser $_.DisplayName -properties ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Its prompting you for the 'Identity' because that is how it identifies what user you are searching for.
Try this:
$users = get-content C:\Temp\test.csv
foreach ($user in $users){Get-ADUser -Identity $user -Properties displayname}
The CSV file just has the user IDs for the users who you would like to find info for.
Or if you can try the following:
Import-Csv C:\Temp\users.csv | ForEach-Object { Get-ADUser -identity $_.Name -Properties displayname }
For a lab working on PowerShell, I have to target a specific OU and list the following information in a text file.
DistinguishedName
DNSHostName
Enabled
Name
ObjectClass
ObjectGUID
SamAccountName
SID
UserPrincipleName
I've found a ton of resources online on how to do this and continuously get an error no matter how I format it.
Here is my code:
$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers DC=uwec, DC=edu'
$Computers = Get-ADComputer -Filter '*' -SearchBase $ou
$Computers | foreach {
$_.DNSHostName
} | Out-File -Filepath "C:\Windows\Temp\Lab7.txt"
I continuously get this error no matter what syntax I use:
Get-ADComputer : The object name has bad syntax
At line:1 char:1
+ Get-ADComputer -Filter '*' -SearchBase 'OU=Testing,OU=Labs,OU=UWEC Co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ADComputer], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
The code you posted does not match the error you posted. However, the most likely reason for the error is a missing comma in your OU:
$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers DC=uwec, DC=edu'
# ^right here
Change that into
$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers,DC=uwec, DC=edu'
and the problem should disappear.
I'm using a PowerShell script to add some information to all users in AD. For some reason, I keep getting an error message if the user has a apostrophe in their name (e.g Tim O'Reilly).
How can I format the script so it will include names with apostrophe ?
My script:
# Import AD Module
Import-Module ActiveDirectory
write-Host 'Starting to update AD Attributes.......' -NoNewline -ForegroundColor Yellow
# Import CSV into variable $users
$users = Import-Csv -Path C:\Scripts\users.csv
# Loop through CSV and update users if the exist in CVS file
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter "displayName -eq '$($user.Name)'" -Properties * -SearchBase "DC=My,DC=domain,DC=com" |
Set-ADUser -Company $($user.Email)
}
Write-Host 'done!' -ForegroundColor Green
And this is the error message I'm getting:
Get-ADUser : Error parsing query: 'displayName -eq 'Tim O'Reilly''
Error Message: 'syntax error' at position: '29'. At
C:\Scripts\Update-information\Update-Users-2.ps1:13 char:1
+ Get-ADUser -Filter "displayName -eq '$($user.Name)'" -Properties * -S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr
osoft.ActiveDirectory.Management.Commands.GetADUser
I'd really appreciate any help I can get here.
Thank you,
You can use some custom delimiter in your csv file instead of modifying your script. Just divide your data with custom char like ":" (Tim : O'Reilly), and add delimiter switch for import-csv cmdlet, like
$users = Import-Csv -Path C:\Scripts\users.csv -Delimiter :
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)'"
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)"