0

我想将 get-vmhost 的输出放到带有时间戳的 csv 文件中。Get-vmhost 命令没有时间戳输出,我尝试执行 timestamp=(get-date),但是当我尝试使用管道时,它给了我一个错误。有什么想法可以在该 csv 文件中插入带有值的时间戳列吗?

if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSsnapin VMware.VimAutomation.Core
}

$Server="dc1-vc.example.com"
Connect-VIServer -server $Server -User <id> -Password <pass>
$Timestamp=(get-date)
Get-VMHost | Where-Object {$_.PowerState -eq "PoweredOn"} | Select-Object Timestamp, Name, NumCpu, CpuUsageMhz, CpuTotalMhz, MemoryUsageGB, MemoryTotalGB |

ConvertTo-Csv  -NoTypeInformation |Out-File -Append -Encoding ascii -FilePath "c:\output\new_dc1.csv"

Disconnect-VIServer -Server $Server -Confirm:$false

错误:

 The term 'Timestamp=' is not recognized as the name of a cmdlet, function, scri
    pt file, or operable program. Check the spelling of the name, or if a path was
    included, verify that the path is correct and try again.
4

2 回答 2

2
$Timestamp=(get-date)
Get-VMHost | Where-Object {$_.PowerState -eq "PoweredOn"} | ForEach-Object{
    "$Timestamp,$($_.Name),$($_.NumCpu),$($_.CpuUsageMhz),$($_.CpuTotalMhz),$($_.MemoryUsageGB),$($_.MemoryTotalGB)" | 
        Out-File -Append -Encoding ascii -FilePath "c:\output\new_dc1.csv"
}

使用您指定的过滤器,使用您想要的属性以及$Timestamp之前创建的属性构建一个以逗号分隔的小字符串。对于每个 Vm,将格式化的代码输出到文件中。

如果您有问题,请告诉我,以便我更新答案。

于 2014-08-23T01:35:42.180 回答
0

或者你可以使用这个:
Get-VMHost | Where-Object {$_.PowerState -eq "PoweredOn"} | Select-Object @{Name="Timestamp";Expression={$Timestamp}}, Name, NumCpu, CpuUsageMhz, CpuTotalMhz, MemoryUsageGB, MemoryTotalGB | ConvertTo-Csv -NoTypeInformation |Out-File -Append -Encoding ascii -FilePath "c:\output\new_dc1.csv"

于 2014-08-23T09:23:14.187 回答