2012-03-12 49 views
2

基于我在这里发布的另一个问题,我能够获得这个脚本的大部分工作,但我不知道如何更改选择对象的格式。谷歌一直没有帮助。输出变量时的条形标题

$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}消失的任何想法。我只想要实际的数据。

回答

2

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

例如:

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

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

而且,如果这是你正在做的和你要使用$time.TIME等你可以有完成:$info.Time

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

和使用等

+0

谢谢你。我确实找到了扩展属性,但没有正确使用它。另外,我喜欢你的最后一点。这将是我做到这一点的方式。这很容易。谢谢。 – 2012-03-12 21:57:05

1

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

它看起来就像你可能会做以下几点:

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

如果要指定,像这样的中间对象的一部分:

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

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

+0

呃...那是我见过的最简单的解决方法。 (我觉得自己有点儿白痴。)但是,谢谢你的帮助。 – 2012-03-12 21:20:42