如何编写对象的单个属性的值? [英] How do I write the value of a single property of a object?
本文介绍了如何编写对象的单个属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我当前的脚本的样子:
This is how my current script looks like:
$cpu = Get-WmiObject win32_processor | select LoadPercentage
logwrite $cpu #this fuction writes $cpu into a .txt file
文件的输出为:
@{LoadPercentage=4}
我只希望它是数字,以便我进行计算.
I want it to be only the number so that I can make calculations.
推荐答案
这是一个非常简单的解决方案.在运行Get-WmiObject时不必选择LoadPercentage,而只需在调用函数时选择属性即可.这只会将数字写入您的日志文件.
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.
$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
这篇关于如何编写对象的单个属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文