将多个输出组合成 table
Combining several outputs into a table
使用 Powershell 我试图将三个命令的结果放入 table 并将它们输出到一个文件,然后永远重复该命令。我不知道如何正确格式化 table。
这是我的脚本
while (1){
$ping = test-connection 8.8.8.8 -delay 1 -count 1
$wifi = @{n='Status';e={get-netadapter -physical -name Wi-Fi | select Status}}
$timestamp = @{n='TimeStamp';e={Get-Date}}
$ping | format-table __SERVER, Address, ResponseTime, $timestamp, $wifi | out-file "C:\test-connection.txt" -append
start-sleep -s 10
}
$ping得到8.8.8.8的ping结果
$wifi 获取 wi-fi 适配器的状态
$timestamp 获取当前时间
最终输出看起来像这样:
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
...
但是,根据我当前的设置,它会这样做:
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 20 18/02/2019 10:19:13 @{Status=Up}
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 19 18/02/2019 10:19:23 @{Status=Up}
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 20 18/02/2019 10:19:33 @{Status=Up}
感谢任何帮助。
您必须在后续写入时隐藏 table headers,并且 trim 输出。
试试这个:
while (1) {
$ping = test-connection 8.8.8.8 -delay 1 -count 1
$wifi = @{n='Status';e={get-netadapter -physical -name Wi-Fi | select -expand Status}}
$timestamp = @{n='TimeStamp';e={Get-Date}}
$path = "C:\test-connection.txt"
$ping | ft __SERVER, Address, ResponseTime, $timestamp, $wifi -Hide:(Test-Path $path) | out-string | % {$_.trim()} | out-file $path -append
start-sleep -s 10
}
尽管使用 Format-Table
进行文件输出并不是一个好主意,它是为了在控制台中显示。考虑使用 CSV 或自定义格式。
使用 Powershell 我试图将三个命令的结果放入 table 并将它们输出到一个文件,然后永远重复该命令。我不知道如何正确格式化 table。
这是我的脚本
while (1){
$ping = test-connection 8.8.8.8 -delay 1 -count 1
$wifi = @{n='Status';e={get-netadapter -physical -name Wi-Fi | select Status}}
$timestamp = @{n='TimeStamp';e={Get-Date}}
$ping | format-table __SERVER, Address, ResponseTime, $timestamp, $wifi | out-file "C:\test-connection.txt" -append
start-sleep -s 10
}
$ping得到8.8.8.8的ping结果
$wifi 获取 wi-fi 适配器的状态
$timestamp 获取当前时间
最终输出看起来像这样:
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
Hostname 8.8.8.8 19 18/02/2019 10:19:23 Up
...
但是,根据我当前的设置,它会这样做:
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 20 18/02/2019 10:19:13 @{Status=Up}
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 19 18/02/2019 10:19:23 @{Status=Up}
__SERVER Address ResponseTime TimeStamp Status
-------- ------- ------------ --------- ------
Hostname 8.8.8.8 20 18/02/2019 10:19:33 @{Status=Up}
感谢任何帮助。
您必须在后续写入时隐藏 table headers,并且 trim 输出。
试试这个:
while (1) {
$ping = test-connection 8.8.8.8 -delay 1 -count 1
$wifi = @{n='Status';e={get-netadapter -physical -name Wi-Fi | select -expand Status}}
$timestamp = @{n='TimeStamp';e={Get-Date}}
$path = "C:\test-connection.txt"
$ping | ft __SERVER, Address, ResponseTime, $timestamp, $wifi -Hide:(Test-Path $path) | out-string | % {$_.trim()} | out-file $path -append
start-sleep -s 10
}
尽管使用 Format-Table
进行文件输出并不是一个好主意,它是为了在控制台中显示。考虑使用 CSV 或自定义格式。