\n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!\nUse a descriptive subject. Don't say \"Need help\" or \"PowerShell Help\", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…\n <\/blockquote>\n<\/aside>\n\n <\/p>","upvoteCount":0,"datePublished":"2018-09-11T16:23:23.000Z","url":"https://community.spiceworks.com/t/ps-script-help/672568/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
try like so, but as said, it depends on your CSV file.<\/p>\n
Import-Module ActiveDirectory\n\n$users = Import-CSV \"c:\\location\\of\\file.csv\"\n\nforeach ($user in $users) {\n\n $gname = $user.givenname\n $sname = $user.surname\n\n if ($gname -and $sname) {\n get-aduser -filter \"givenname -eq '$gname' -and surname -eq '$sname'\" -properties * | \n select samaccountname, emailaddress\n }\n else {\n Write-Warning \"no name info\"\n }\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-09-11T16:25:47.000Z","url":"https://community.spiceworks.com/t/ps-script-help/672568/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":" <\/p>","upvoteCount":0,"datePublished":"2018-09-11T16:34:55.000Z","url":"https://community.spiceworks.com/t/ps-script-help/672568/5","author":{"@type":"Person","name":"rekin","url":"https://community.spiceworks.com/u/rekin"}},{"@type":"Answer","text":"
JitenSh,<\/p>\n
Thanks.<\/p>","upvoteCount":0,"datePublished":"2018-09-17T16:01:00.000Z","url":"https://community.spiceworks.com/t/ps-script-help/672568/7","author":{"@type":"Person","name":"rekin","url":"https://community.spiceworks.com/u/rekin"}}]}}
rekin
(rekin)
September 11, 2018, 4:15pm
1
Hi,
I have written a simple PS script which will pull user data such as samaccountname and emailaddress using first and last names pulled from a CSV file. The script is below:
Import-Module ActiveDirectory
$users = Import-CSV “locationofcsvfile”
foreach ($user in $users) {
$gname = $user.givenname
$sname = $user.surname
get-aduser -filter {givenname -eq $gname -and surname -eq $sname} -properties * | select samaccountname,emailaddress
}
The following errors appear when running the script:
Please tell me what I am missing…
Thanks!
1 Spice up
shelly
(Shelly3360)
September 11, 2018, 4:22pm
2
What is the structure of your CSV file? Does its first row have titles? Can you post a small example - sanitized of course.
1 Spice up
Neally
(Neally)
September 11, 2018, 4:23pm
3
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don't say "Need help" or "PowerShell Help", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…
Neally
(Neally)
September 11, 2018, 4:25pm
4
try like so, but as said, it depends on your CSV file.
Import-Module ActiveDirectory
$users = Import-CSV "c:\location\of\file.csv"
foreach ($user in $users) {
$gname = $user.givenname
$sname = $user.surname
if ($gname -and $sname) {
get-aduser -filter "givenname -eq '$gname' -and surname -eq '$sname'" -properties * |
select samaccountname, emailaddress
}
else {
Write-Warning "no name info"
}
}
rekin
(rekin)
September 11, 2018, 4:34pm
5
jitensh
(JitenSh)
September 11, 2018, 4:35pm
6
CSV should have a header like givename,surname based on your code you running. Looks you have gname and sname so it will be
Import-Module ActiveDirectory
$users = Import-CSV "c:\location\of\file.csv"
foreach ($user in $users) {
$gname = $user.gname
$sname = $user.sname
$user=Get-ADUser -Filter "GivenName -eq '$gname' -and Surname -like '$sname'" -properties Emailaddress
if( $User )
{
$user |select samaccountname, emailaddress
}
else
{
New-Object PSObject -Property @{
GivenName = $Gname
Surname = $sname
Status = 'MISSING ACCOUNT'
}
}
}
4 Spice ups
rekin
(rekin)
September 17, 2018, 4:01pm
7