?
Solved

Searching an imported .CSV in powershell

Posted on 2009-05-08
3
Medium Priority
?
3,221 Views
Last Modified: 2012-06-27
I'm trying to import a simple .CSV file into Powershell using import-csv, then search it for a specific string, and display the description of the object that matches the string.

CSV File looks like this:
Name,Description
Server1,Server1 description
Server2,Server2 Description

So basically, I want to search through the list for server2, and display server2's description. It doesn't seem to like how I'm doing it however, because it doesn't display anything. Attached is the code I'm trying to use to do it.

Thanks in advance!
cls
$Serverlist = (Import-Csv "d:\serverlist.csv")
Write-Host ($Serverlist.description | where{$serverlist.name -match "server2"})

Open in new window

0
Comment
Question by:tilbard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
tilbard earned 0 total points
ID: 24336038
Needed a foreach loop in there to process the whole of $serverlist.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24336068

This will return the Description if you have multiple matches for your Where-Object search.

$ServerList = Import-CSV "d:\serverlist.csv"
$ServerList | ?{ $_.Name -Match "server2" } | Select-Object Description

Or if the search returns a single result you can access just that element (implicit conversion from array of objects to object):

($ServerList | ?{ $_.Name -Match "server2" }).Description

Chris
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24336085

You didn't need a ForEach loop, it really depends on what you're trying to see :) If you wanted all Descriptions as simple strings you would need ForEach. e.g.:

$ServerList | %{ $_.Description }

Chris
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question