Computer,Team
server,team1
server2,team2
server3,team2
我希望将此列表导入PS。我使用以下命令,我将用它来对每个计算机名称执行查询:
$Serverlist = import-csv "c:\temp\appsupportAll.txt"
$Servers = $Serverlist | Select Computer
Foreach ($Computer in $Servers)
{etc etc}
$ Serverlist和$ Servers的输出正如我所料。但是,每台$ Computer都会以PS格式呈现给PS:
@{$_.Computer=server}
@{$_.Computer=server2}
@{$_.Computer=server3}
etc
我正在寻找"服务器"," server2"," server3"等等。我做错了什么?
由于
答案 0 :(得分:3)
$Computer
不仅仅是您正在寻找的价值。它本质上是一个自定义对象,其中一个属性称为“计算机”。如果您只想要单个“计算机”属性的字符串表示形式,请使用select -ExpandProperty
,如下所示:
$Serverlist = import-csv "c:\temp\appsupportAll.txt"
$Servers = $Serverlist | Select -ExpandProperty Computer
Foreach ($Computer in $Servers)
{etc etc}
或更简单地说,你可以写下这个:
$Serverlist = import-csv "c:\temp\appsupportAll.txt"
Foreach ($Computer in $serverlist.Computer)
{etc etc}
或者,如果您需要更多属性:
$Serverlist = import-csv "c:\temp\appsupportAll.txt"
foreach ($server in $serverlist) {
$server.team
$server.computer
}
答案 1 :(得分:0)
如果您的输入文件没有标题,请改用它:
$Serverlist = Get-Content "c:\temp\appsupportAll.txt"
$Serverlist | % {whatever you need}