我有一个小脚本,它读出一个注册表路径,创建一个输出作为PSCUstomObject并在控制台中显示它,并将其记录到一个文件中。
这是剧本:
$LastLicense = 0
while ($true)
{
[int]$lizenzen = gp "HKCU:\SOFTWARE\Logik GmbH\Logik\Execution" -name Licenses -ea Stop |
select -Expand Licenses
if ($lizenzen -ne $LastLicense)
{
[PSCustomObject] @{
"DateTime" = (get-date).DateTime
"Lizenzen in Gebrauch" = $lizenzen
} | tee -FilePath C:\install\LizenzLog.txt -Append | ft -AutoSize
$LastLicense = $lizenzen
}
sleep -s 10
}
现在输出如下:
DateTime Lizenzen in Gebrauch
-------- --------------------
2017.07.04 11:01:28 5
DateTime Lizenzen in Gebrauch
-------- --------------------
2017.07.04 11:01:28 6
DateTime Lizenzen in Gebrauch
-------- --------------------
2017.07.04 11:01:28 5
但我想这样:
DateTime Lizenzen in Gebrauch
-------- --------------------
2017.07.04 11:01:28 5
2017.07.04 11:01:28 6
2017.07.04 11:01:28 5
这可能吗?
答案 0 :(得分:0)
我认为在这种情况下,您可以跳过[pscustomobject]
创建,只需创建一个将保存在CSV文件中的字符串:
#Create CSV file with Separator char for Excel and headers, if file exists will be replaced with empty one
"SEP=," | out-file -FilePath C:\install\LizenzLog.csv -Force
"DateTime" + "," + "Lizenzen in Gebrauch" | out-file -FilePath C:\install\LizenzLog.csv -Append
while ($true)
{
[int]$lizenzen = gp "HKCU:\SOFTWARE\Logik GmbH\Logik\Execution" -name Licenses -ea Stop | % Licenses
if ($lizenzen -ne $LastLicense)
{ #export values as string elements delimited by comma, easy way to import to Excel (just open csv file it should open it corectly)
"$((get-date).DateTime)"+ "," + $lizenzen | tee -FilePath C:\install\LizenzLog.csv -Append
$LastLicense = $lizenzen
}
sleep -s 10
}
通过这种方式,您将获得一个可以导入Excel的CSV文件 - 如果您只是双击,则应立即导入列。
如果要在控制台窗口中查看其演示文稿,请在脚本执行后执行
只需导入CSV:Import-Csv C:\install\LizenzLog.csv