请告诉我如何添加csv文件,以便将每个变量的数据写入所需列?对于每一个新的循环到一个新的行?
New-Item -Path . -Name "Test.csv" -ItemType "file" -force | Out-Null
("" | Select-Object Date, RAM_1, RAM_2, RAM_3, RAM_Used_total_%, RAM_Free%, CPU_Load% | ConvertTo-Csv -NoType -Delimiter ";")[0] | Out-File '.Test.csv'
# Start the while loops
while($true) {
# In which data is collected into variables that need to be entered into each of the columns, respectively
$date
$RAM_1
$RAM_2
$RAM_3
$RAM_Usage
$Free_RAM
$CPU_Load
Start-Sleep -s 60
}
提前谢谢你
不要尝试手动编写CSV文件-让Export-CSV
为您处理:
while($true) {
# populate your variables ...
$date = $(<# ... #>)
$RAM_1 = $(<# ... #>)
$RAM_2 = $(<# ... #>)
$RAM_3 = $(<# ... #>)
$RAM_Usage = $(<# ... #>)
$Free_RAM = $(<# ... #>)
$CPU_Load = $(<# ... #>)
# then create a new object with the correct column names
$record = [pscustomobject]@{
'Date' = $date
'RAM_1' = $RAM_1
'RAM_2' = $RAM_2
'RAM_3' = $RAM_3
'RAM_Used_total_%' = $RAM_Usage
'RAM_Free%' = $Free_RAM
'CPU_Load%' = $CPU_Load
}
# append record to CSV
$record |Export-Csv .pathtooutput.csv -Append
Start-Sleep -s 60
}