首页 > 解决方案 > I want to use get-counter ”\\$env:COMPUTERNAME\memory\% committed bytes in use” to append to a csv in the next line

问题描述

How can i append the get-counter into the cell in my csv without overwriting the current data.

$path = "v:\logg\logg.csv"
$ram = Get-Counter "\\$env:COMPUTERNAME\minne\% använda dedikerade byte"
$drivers = Get-Volume |Where-Object {$_.DriveLetter-ne $null}| select DriveLetter,Size,SizeRemaining,@{e={$_.Size - $_.SizeRemaining};l="Used"},@{e={[math]::Round(100 / ($_.size / $_.SizeRemaining) )};l="Total %"} 
$drivers | Export-Csv $path -NoTypeInformation -Append -Force
Get-Date -Format "yyyy-MM-dd HH:mm"|Out-File $path -Append

I want it to look like this but with the get-counter added to the next empty cell:

enter image description here

If I try to add the $ram with add-content or out-file it will overwrite the whole file and remove the $drivers

If i try to do it like this:

$path = "v:\logg\hej.csv"
$ram = Get-Counter "\\$env:COMPUTERNAME\minne\% använda dedikerade byte"
$ram |Export-Csv $path -NoTypeInformation -Append -Force

$drivers = Get-Volume |Where-Object {$_.DriveLetter-ne $null}| select DriveLetter,Size,SizeRemaining,@{e={$_.Size - $_.SizeRemaining};l="Used"},@{e={[math]::Round(100 / ($_.size / $_.SizeRemaining) )};l="Total %"} 
$drivers | Export-Csv $path -NoTypeInformation -Append -Force

It will look like this, overwriting the whole part of $drivers

标签: powershellcsvappend

解决方案


仅对于提交的字节%,您可以使用它...

$Counter = '\\{0}{1}' -f $env:COMPUTERNAME, '\Memory\% Committed Bytes in Use'
$RAM_CommBytesInUse_Pct = [int](get-counter -Counter $Counter).
    CounterSamples[0].
    CookedValue

刚才在我的系统上输出=39

但是,当您添加所有这些不具有相同列的东西时,您并没有制作 CSV 文件。例如,您插入的日期是DriveLetter列中的字符串。要制作真正的 CSV 文件,您需要在新对象中重新排列属性。


推荐阅读