Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, November 16, 2018 12:19 AM
Hi ,
I am trying to check if samaccountname exist in AD,. my csv file has header samaccountname
but it seems my variables are not working . any clues
$data = Import-Csv -Path "C:\psdump\myusers.csv"
foreach ($samaccountname in $data) {
try {
get-aduser -filter "samaccountname -eq $($_.samaccountname)" -erroraction stop
write-host "User '$($_.samaccountname)' exists in AD."
Out-file C:\psdump\exists.txt -append -inputobject "User '$($_.samaccountname)' exists in AD." `
}
catch {
write-host "User '$($_.samaccountname)' DOES NOT exist in AD."
Out-file -inputobject "User '$($_.samaccountname)' DOES NOT exist in AD." `
C:\psdump\not-exists.txt -append
}
}
All replies (6)
Friday, November 16, 2018 2:12 AM ✅Answered
In the original script, if the CSV file has the header line sAMAccountName, and a value is jsmith, then $sAMAcountName will be @{sAMAccountName=jsmith}. The expression $($_.sAMAccountName) will be blank. But $sAMAccountName.sAMAccountName will be jsmith.
Richard Mueller - MVP Enterprise Mobility (Identity and Access)
Yes. My error. I copied some code and failed to update it. I have fixed the original post.
What Richard is saying is to change "$_" to "$SamAccountName" or whatever variable you used to enumerate the CSVwhich is why I prefer doing it this way:
Import-Csv C:\psdump\myusers.csv |
ForEach-Object{
if(get-aduser -filter "samaccountname -eq '$($_.samaccountname)'"){
$msgtxt = "User $($_.samaccountname) exists in AD."
write-host $msgtxt -Fore Green
$msgtxt | Out-file C:\psdump\exists.txt -append
}else{
$msgtxt = "User $($_.samaccountname) DOES NOT exist in AD."
write-host $msgtxt -fore Cyan
$msgtxt | Out-file C:\psdump\not-exists.txt -append
}
}
Understanding variables in all context is critical to using PowerShell. My gross oversights certainly are not helpful.
\(ツ)_/
Friday, November 16, 2018 1:07 AM
Unfortunately you are both wrong. This one is always a "gotcha" because of the filter's weirdness.
Fist note that with a filter you will never get an exception.
$data = Import-Csv C:\psdump\myusers.csv
foreach ($samaccountname in $data) {
if(get-aduser -filter "samaccountname -eq '$($samaccountname .samaccountname)'"){
$msgtxt = "User $($_.samaccountname) exists in AD."
write-host $msgtxt -Fore Green
$msgtxt | Out-file C:\psdump\exists.txt -append
}else{
$msgtxt = "User $($_.samaccountname) DOES NOT exist in AD."
write-host $msgtxt -fore Cyan
$msgtxt | Out-file C:\psdump\not-exists.txt -append
}
}
Single quotes are required in the filter.
\(ツ)_/
Friday, November 16, 2018 1:43 AM
still having problems... it seems the
get-aduser : The search filter cannot be recognized
At line:4 char:9
+ if (get-aduser -filter "samaccountname -eq '$($_.samaccountname)' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Friday, November 16, 2018 1:46 AM
You obviously have a typo somewhere or there is no field named "SamAccountName" in your CSV.
Post the first few lines of your CSV.
Remember that a CSV has headers with column names and a very specific format. It is not just a simple text file. JUst adding a CSV extension to a file does not make it a CSV file.
\(ツ)_/
Friday, November 16, 2018 12:51 PM
Awesome, you guys rock!
Been working on this for 2 days :(
If I may ask one more question. I am struggling to understand when to use double and single quotes any pointers would be appreciated
Friday, November 16, 2018 1:11 PM
help about_Quoting_Rules
single quotes are the primary quoting method in PowerShell even though non-trained people keep using double quotes. Double quoted strings are called expandable strings because the contents of the string are parsed by PowerShell and any key elements or expressions are expanded if possible.
It is a good practice to avoid double quotes unless you really are needing the string to be altered (expanded) at runtime.
\(ツ)_/