Powershell: showing value only instead of @{} in r

2019-08-16 06:50发布

问题:

I'm new to PWSH, it may looks easy for you guys but I can't find the way to show the result as expected.

$vm2 = Get-VM VM1234
$vmMemUsageAvg = get-VM $vm2.Name | Select @{N="Mem.Usage.Average";E={[Math]::Round((($_ |Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) |Measure-Object Value -Average).Average),2)}}

Now, as example, when requesting the result of $vmMemUsageAvg I receive this output:

C:\Windows\system32> $vmMemUsageAvg 
Mem.Usage.Average
-----------------
              5.9

but I want only want as output the value, like: 5.9

putting that in Excel cell with

$Sheet.Cells.Item($intRow, 13) = [String]$vmMemUsageAvg 

gives me a string like this:

@{Mem.Usage.Average=5.9}

Any help welcome to have this value ONLY displayed

回答1:

By using Select-Object @{N="Mem.Usage.Average";E={...}} you are making an object with a property Mem.Usage.Average, but it looks just want a string. So just take out the Select

$vm2 = Get-VM VM1234
$vmMemUsageAvg = [Math]::Round(((get-VM $vm2.Name | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-24)-IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)