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 に答える