Searching Imported CSV Array with Variable

Afternoon all!

Long time reader, first time poster!

Self taught powersheller here so apologies if I’ve missed something super fundamental (had a search around but to no avail, again though, may not be phrasing my searches quite right due to levels of noob)

High level overview: Importing a basic 2 column csv into powershell and then querying it with a $variable to return a value

Sample CSV:

Gateway Site AA BB CC
I'm getting Gateway from a wmiobject query and assigning it to $Gateway however when I search for it against the array it comes back with nothing?

If I manually set $Gateway = the query returns AA as expected?


$GlobalSites = Import-Csv -Path C:\SITES.csv
$Gateway = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where {($.IPEnabled -eq $True) -and ($.IPAddress -ne $null)}).DefaultIPGateway | Out-String
$LocCode = $GlobalSites.where({$_.Gateway -eq "$Gateway"}).site

I’m almost certain its a gap/misunderstanding in my knowledge or I’m overcomplicating things.

Any help\guidance is much appreciated.


You need to validate that the generated $Gateway is what you expect. In this case the $Gateway has some extra spaces.

$Gateway = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where {($_.IPEnabled -eq $True) -and ($_.IPAddress -ne $null)}).DefaultIPGateway | Out-String
# Before Trim
($Gateway | Measure-Object -Character).Characters

# After Trim
$Gateway = $Gateway.Trim()
($Gateway | Measure-Object -Character).Characters

@p0rkjello You are a scholar and a gentleman sir!

Every day is a school day!

Many thanks!