我有一个看起来像这样的CSV文件
WRK-STATION-1, Room 54 WRK-STATION-2, Room 12 WRK-STATION-3, Room 45 WRK-STATION-4, Room 11 WRK-STATION-5, Room 32
我当前的代码假设我的CSV文件中只有一列,代码如下所示:
$computerlist = Get-Content $COMPUTERLIST -ErrorAction Stop
foreach ($computer in $computerlist) {
if ((Test-Connection -ComputerName $computer -Quiet) -eq $true) {
# some stuff happens here
# I would like to Output to the screen "The computer with tha name
# WRK-STATION-1 in Room 54 has been completed"
# Or create an output file that adds a 3rd column to my input saying
# "COMPLETED"
} else {
# I would like to Output to the screen "The computer with tha name
# WRK-STATION-1 in Room 54 has failed"
# Or create an output file that adds a 3rd column to my input saying
# "FAILED"
}
我想弄清楚如何让它理解有2列。
答案 0 :(得分:2)
使用Import-Csv
代替Get-Content
。如果输入文件没有标题行,您可以通过-Header
参数指定列标题。
$computerlist = Import-Csv $COMPUTERLIST -Header Name,Room -ErrorAction Stop
这样你可以像这样使用它:
foreach ($computer in $computerlist) {
if (Test-Connection -ComputerName $computer.Name -Quiet) {
...
} else {
"Computer $($computer.Name) in $($computer.Room) has failed."
}
}
答案 1 :(得分:0)
也许,你需要这样的东西吗?
$computerlist = Get-Content $COMPUTERLIST -ErrorAction Stop
foreach ($computerInfo in $computerlist) {
$computer = $computerInfo -split ',' | select -First 1
if((Test-Connection -ComputerName $computer -Quiet) -eq $true) {
# some stuff happens here
# I would like to Output to the screen "The computer with tha name WRK-STATION-1 in Room 54 has been completed"
# Or create an output file that adds a 3rd column to my input saying "COMPLETED"
}
else {
# I would like to Output to the screen "The computer with tha name WRK-STATION-1 in Room 54 has failed"
# Or create an output file that adds a 3rd column to my input saying "FAILED"
}