我无法将此命令格式化为字符串。下面的代码将$r
作为空字符串返回。为什么呢?
$f = gwmi -ComputerName PCname -Class Win32_Process -Filter "name='process.exe'"|
select {$_.WorkingSetSize/1MB}|fl
$r = $f.ToString()
答案 0 :(得分:0)
如果检查Format-List返回的对象类型,您将看到它是System.Object的数组。
$f = gwmi -ComputerName PCname -Class Win32_Process -Filter "name='process.exe'"| `
select {$_.WorkingSetSize/1MB}|fl
$r | get-member
如果您只需要工作集的值,请尝试:
gwmi -ComputerName PCname -Class Win32_Process -Filter "name='process.exe'" | `
select @{label='WorkingSetSize';expression={$_.WorkingSetSize/1MB}}| `
select -expandproperty WorkingSetSize
答案 1 :(得分:0)
Format-List
生成一个字符串数组,而不是单个字符串,因此$f.ToString()
应该为您提供System.Object[]
。要将数组转换为单个字符串,可以将其传递到Out-String
cmdlet:
$f = gwmi -ComputerName PCname -Class Win32_Process -Filter "name='process.exe'" |
select {$_.WorkingSetSize/1MB} | fl
$g = $f | Out-String
但是,如上所述,您至少应该获得System.Object[]
,而不是空字符串,因此请仔细检查$f
的值。
答案 2 :(得分:0)
我发现-Filter“name ='process.exe'”对我不起作用。所以我替换了
| where {$_.name -match "Process"}
这是我的结果,我还将LocalHost替换为PCname并删除了| fl
$r = gwmi -ComputerName localhost -Class Win32_Process | where {$_.name -match "Process"} |
select @{label='WorkingSetSize';expression={$_.WorkingSetSize/1MB}} |
select -expandproperty WorkingSetSize
$r.ToString()