Searching Imported CSV Array with Variable

Welcome Forums General PowerShell Q&A Searching Imported CSV Array with Variable

Viewing 2 reply threads
  • Author
    Posts
    • #236506
      Participant
      Topics: 1
      Replies: 1
      Points: -6
      Rank: Member

      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
      192.168.0.1 AA
      192.168.0.2 BB
      192.168.0.3 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 = 192.168.0.1 the query returns AA as expected?

      Code:

      $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
      $LocCode

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

      Any help\guidance is much appreciated.

      Thanks

    • #236515
      Participant
      Topics: 0
      Replies: 3
      Points: 3
      Rank: Member

      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
      11
      9
    • #236842
      Participant
      Topics: 1
      Replies: 1
      Points: -6
      Rank: Member

      @p0rkjello You are a scholar and a gentleman sir!

      Every day is a school day!

      Many thanks!

Viewing 2 reply threads
  • You must be logged in to reply to this topic.