$timeout = new-timespan -Minutes 1
$sw = [diagnostics.stopwatch]::StartNew()
$path = "d:\powershell\test.csv"
"Processor Load, Available Memory(MB), Max Memory(Bytes)" >> $path
while ($sw.elapsed -lt $timeout)
{
$a = gwmi -query "Select * from win32_processor"
$b = gwmi -query "Select * from win32_perfrawdata_perfOS_memory"
$c = gwmi -query "Select * from win32_physicalmemory"
$date = Get-Date -format s
$a.loadpercentage + "," + $b.availableMbytes + "," + $c.capacity >> $path
start-sleep -seconds 5
}
因此,我只是想獲取有關正在發生的事情的分鐘快照。我不只是因爲理由而在perfmon中打開它。基本上我希望在提到的CSV文件中得到以逗號分隔的輸出。它適用於單個變量,但是當我嘗試添加第二個變量或文本時,出現以下錯誤。連接PowerShell變量
Cannot convert value ", test" to type "System.Int32". Error: "Input string was not in a
correct format."
At D:\powershell\VDIPerfMon.ps1:14 char:21
+ $a.loadpercentage + <<<< ", test" >> $path
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
我該如何解決這個問題?
我使用PowerShell的連接運算符'-j'給了一個簡單的答案,但我會建議使用哈希表創建一個自定義對象,然後將其發送到Export-CSV – Eris