解析多字段CSV文件
我有一个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文件,csv,powershell,Csv,Powershell,我有一个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 我想弄清楚如何让它理解有两列。使用而不是获取内容。如果输入文件没有标题行,可以通过-header参数指定列标题 $computerlist = Import-Csv $COMPUTERLIST -Header Name,Room -Er
获取内容
。如果输入文件没有标题行,可以通过-header
参数指定列标题
$computerlist = Import-Csv $COMPUTERLIST -Header Name,Room -ErrorAction Stop
这样,您可以像这样使用它:
$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"
}
foreach ($computer in $computerlist) {
if (Test-Connection -ComputerName $computer.Name -Quiet) {
...
} else {
"Computer $($computer.Name) in $($computer.Room) has failed."
}
}
使用而不是获取内容
。如果输入文件没有标题行,可以通过-header
参数指定列标题
$computerlist = Import-Csv $COMPUTERLIST -Header Name,Room -ErrorAction Stop
这样,您可以像这样使用它:
$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"
}
foreach ($computer in $computerlist) {
if (Test-Connection -ComputerName $computer.Name -Quiet) {
...
} else {
"Computer $($computer.Name) in $($computer.Room) has failed."
}
}
也许,你需要这样的东西
$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"
}
也许,你需要这样的东西
$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"
}
谢谢,但我如何访问第二个字段?因为foreach($computerlist中的computer){将被破坏,因为答案中有2个字段而不是1。您通过说出$computer.name访问第一个字段,然后通过说出$computer.room访问第二个字段。名称“name”和“room”是任意选择,在import csv语句的-header子句中声明。我在我的csv文件中创建了一个标题,并使用了与您相同的语法。它使用标题作为我的测试连接的第一行。是否要跳过它?请再次阅读:如果输入文件没有标题行,您可以通过
-Head指定列标题er
参数。谢谢,但是如何访问第二个字段?因为foreach($computerlist中的computer){将被破坏,因为答案中有2个字段而不是1。您通过说出$computer.name访问第一个字段,通过说出$computer.room访问第二个字段。名称“name”和“room”是任意选择,在import csv语句的-header子句中声明。我在我的csv文件中创建了一个标题,并使用了与您相同的语法。它使用标题作为我的测试连接的第一行。是否要跳过它?请再次阅读:如果输入文件没有标题行,您可以通过-Head指定列标题er
参数。