【问题标题】:Strip Header When Outputting Variables输出变量时去除标题
【发布时间】:2012-03-12 21:01:51
【问题描述】:

根据我在此处发布的另一个问题,我能够使该脚本的大部分工作,但我不知道如何更改选择对象的格式。谷歌一直没有帮助。

$head = @'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Current Conditions</title>
    </head>

    <body style="background-color:#EEEEEE">
    <h3 style="margin:0;padding-bottom:3px">Current Conditions at 
'@

$1 = @'
</h3>
    <p style="margin:0;padding-bottom:0px"><b>Dewpoint: </b>
'@

$2 = @'
</p>
    <p style="margin:0;padding-bottom:0px"><b>Temperature: </b>
'@

$3 = @'
</p>
    <p style="margin:0;padding-bottom:3px"><b>Humidity: </b>
'@

$end = @'
</p>
    </body>
</html>
'@

$info = import-csv 1-pastweek.csv
$time = $info | select -last 1 Time
$temp = $info | select -last 1 Temp
$humid = $info | select -last 1 Humid
$dewpt = $info | select -last 1 Dewpt

$head + $time + $1 + $dewpt + $2 + $temp + $3 + $humid + $end | out-file -encoding "UTF8" current.html

这确实有效,但 html 输出的格式包括来自 CSV 的标头信息,我不知道如何解决它。

Current Conditions at @{TIME=03/12/2012 15:32:22}

Dewpoint: @{DEWPT=62.7}

Temperature: @{TEMP=71.9}

Humidity: @{HUMID=74.4}

关于如何让 @{HEADER } 消失的任何想法。我只想要实际数据。

【问题讨论】:

    标签: html powershell csv


    【解决方案1】:

    对于您所尝试的,您只需添加 -expandproperty 即可获取实际值,而不是具有该属性的对象。

    例如:

    $time = $info | select -last 1 -expand Time
    

    你可以直接使用$time,而不是$time.TIME

    另外,如果这就是您正在做的所有事情并且您将使用$time.TIME 等,您可以这样做:

    $info = import-csv 1-pastweek.cs | select -last 1
    

    并使用$info.Time等。

    【讨论】:

    • 谢谢你。我确实找到了 expandproperty,但没有正确使用它。另外,我喜欢你的最后一点。我就是这样做的。那更容易。谢谢。
    【解决方案2】:

    您发布的所有内容都是正确的,您使用什么代码来组合这些字符串?

    看起来您可能正在执行以下操作:

    $head + $time + $1 + $dewpt + $2 + $temp + $3 + $humid + $end
    

    当你想像这样指定中间对象的一部分时:

    $head + $time.TIME + $1 + $dewpt.DEWPT + $2 + $temp.TEMP + $3 + $humid.HUMID + $end
    

    此语法假定您使用哈希表 (@{}) 来保存您的对象。

    【讨论】:

    • 嗯...这是我见过的最简单的修复方法。 (我觉得自己有点白痴。)但是,非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多