Reputation: 741
relative newbie on PS. I have a simple CSV file:
Computer,Team
server,team1
server2,team2
server3,team2
and I wish to import this list into PS. I use the following command, which I will use to perform queries against each computer name:
$Serverlist = import-csv "c:\temp\appsupportAll.txt"
$Servers = $Serverlist | Select Computer
Foreach ($Computer in $Servers)
{etc etc}
The output of $Serverlist and $Servers is as I expect it. However, each $Computer is presented to PS as:
@{$_.Computer=server}
@{$_.Computer=server2}
@{$_.Computer=server3}
etc
I am looking for "server", "server2", "server3", etc. What am I doing wrong please?
Thanks
Upvotes: 4
Views: 1370
Reputation: 11
If your input file does not have headers, use this instead:
$Serverlist = Get-Content "c:\temp\appsupportAll.txt"
$Serverlist | % {whatever you need}
Upvotes: 0
Reputation: 97861
$Computer
inside your foreach loop is not just the value you're looking for. It is, in essence, a custom object with one property called "Computer". If you just want the string representation of the single "Computer" property, use select -ExpandProperty
like so:
$Serverlist = import-csv "c:\temp\appsupportAll.txt"
$Servers = $Serverlist | Select -ExpandProperty Computer
Foreach ($Computer in $Servers)
{etc etc}
or even more simply, you can just write this:
$Serverlist = import-csv "c:\temp\appsupportAll.txt"
Foreach ($Computer in $serverlist.Computer)
{etc etc}
or, if you need more properties:
$Serverlist = import-csv "c:\temp\appsupportAll.txt"
foreach ($server in $serverlist) {
$server.team
$server.computer
}
Upvotes: 3