Question:
This is how my current script looks like:
1 2 3 |
$cpu = Get-WmiObject win32_processor | select LoadPercentage logwrite $cpu #this fuction writes $cpu into a .txt file |
The output of the file is:
1 2 |
@{LoadPercentage=4} |
I want it to be only the number so that I can make calculations.
Answer:
That is a pretty simple fix. Instead of selecting the LoadPercentage
when running Get-WmiObject
, just select the property when calling your function. This will write only the number to your log file.
1 2 3 4 5 6 7 8 9 10 |
$cpulogpath = "C:\Monitoring\$date.csv" function logwrite { param ([string]$logstring) add-content $cpulogpath -value $logstring } $cpu = Get-WmiObject win32_processor #don't select the property here logwrite $cpu.LoadPercentage #select it here |