Author Posts

January 1, 2012 at 12:00 am

by pstosso at 2013-03-23 13:31:41

I have a csv file that looks like the following...

"SamAccountName", "Name"
"tosso", "osso, tater J"

I want to enter the Name ("osso, tater") and be able to get the SamAccountName from the csv file and store it in a variable.
I'm having issues with the SamAccountName.

$user = Read-Host 'Enter user name'

$irmusername = Import-Csv $source\script\irmuser.csv

by poshoholic at 2013-03-23 15:41:47

What you need to do is create a hash table from your CSV. Something like this:
# Create the lookup table from the csv file contents
$lookupTable = @{}
Import-Csv $source\script\irmuser.csv | ForEach-Object {
$lookupTable[$_.Name] = $_.SamAccountName
}
# Now prompt the user for a username
$user = Read-Host 'Enter user name'
# Find the SAM account name for the user, first using exact match, then using partial match if there is only one that matches
if ($lookupTable.Keys -contains $user) {
"The SAM Account Name for $user is $($lookupTable[$user])."
} elseif ($possibleMatches = @($lookupTable.Keys -like "*$user*")) {
if ($possibleMatches.Count -eq 1) {
"The SAM Account Name for $($possibleMatches[0]) is $($lookupTable[$possibleMatches[0]])."
} else {
"The username you provided ($user) is ambiguous. The following users were found as possible matches: $($possibleMatches -join ','). Please try again with a more specific username."
}
} else {
"User $user was not found."
}

NOTE: I have not tested this code. I just put this together as one possible way to approach the problem. There may be a typo or two.

by mjolinor at 2013-03-23 16:40:07

I've gotten to like that -OutputMode switch in V3's Out-Gridview

$irmusername = Import-Csv $source\script\irmuser.csv

$SamAccount = $irmusername |
where {$_.Name -match (Read-Host "Enter user name match string")} |
Out-GridView -Title "Select User" -OutputMode Single |
select -ExpandProperty SamAccountName

by pstosso at 2013-03-23 17:49:07

Thanks for the quick reply. The lookup table works great.

by pstosso at 2013-03-23 17:51:24

[quote="poshoholic"]What you need to do is create a hash table from your CSV. Something like this:
# Create the lookup table from the csv file contents
$lookupTable = @{}
Import-Csv $source\script\irmuser.csv | ForEach-Object {
$lookupTable[$_.Name] = $_.SamAccountName
}
# Now prompt the user for a username
$user = Read-Host 'Enter user name'
# Find the SAM account name for the user, first using exact match, then using partial match if there is only one that matches
if ($lookupTable.Keys -contains $user) {
"The SAM Account Name for $user is $($lookupTable[$user])."
} elseif ($possibleMatches = @($lookupTable.Keys -like "*$user*")) {
if ($possibleMatches.Count -eq 1) {
"The SAM Account Name for $($possibleMatches[0]) is $($lookupTable[$possibleMatches[0]])."
} else {
"The username you provided ($user) is ambiguous. The following users were found as possible matches: $($possibleMatches -join ','). Please try again with a more specific username."
}
} else {
"User $user was not found."
}

NOTE: I have not tested this code. I just put this together as one possible way to approach the problem. There may be a typo or two.[/quote]

I did have to store that SamaccountName found in a variable but that wasn't hard at all. Thank you. This helps me out so much.

by poshoholic at 2013-03-24 11:38:04

My pleasure. I'm glad I was able to help.