Power shell outputs curly brackets instead of actu

2020-05-09 17:06发布

Below is the code I have which is reproducible on your system as well :

$object=New-Object psobject
$computerName ='servername'

$Ostype = (Get-WmiObject  -Class Win32_OperatingSystem  -ComputerName $computerName -ErrorAction SilentlyContinue ).Caption  
$PhysicalCores = Get-WmiObject   -Class Win32_processor -ComputerName $computerName | Measure-Object -Property Numberofcores -Sum | select Sum
$LogicalCores = Get-WmiObject   -Class Win32_processor  -ComputerName $computerName | Measure-Object -Property NumberofLogicalProcessors -Sum | select Sum
$NumberofSockets=Get-WmiObject -Class win32_processor -ComputerName $computerName| Measure-Object -Property socketdesignation |select Count
$computerram=(Get-WMIObject -class Win32_PhysicalMemory -ComputerName $computerName   |Measure-Object -Property capacity -Sum | % {[Math]::Round(($_.sum / 1GB),2)})
$type=get-wmiobject win32_computersystem -ComputerName $computerName |select model



$object=New-Object psobject
$object | Add-Member -MemberType NoteProperty -Name ComputerName -Value $computerrealname
$object | Add-Member -MemberType NoteProperty -Name ComputerType -Value $Computertype
$object | Add-Member -MemberType NoteProperty -Name OSType  -Value $Ostype
$object | Add-Member -MemberType NoteProperty -Name TotalMemory_GB -Value $computerram
$object | Add-Member -MemberType NoteProperty -name NumberofProcessors -Value $PhysicalCores
$object | Add-Member -MemberType NoteProperty -name NumberofLogicalProcessors -Value $LogicalCores
$object | Add-Member -MemberType NoteProperty -name NumberofSockets -Value $NumberofSockets
$object | Add-Member -MemberType NoteProperty -name MachineType -Value $type



$object | Out-GridView

Output is like below

enter image description here

As you can see, output for some of the variables in showing in curly brackets, how to remove them?

Things I have tried
1. Without adding variables to object, output doesn't come in curly brackets
2. Tried doing an sum.toint32() but no luck

Can some one please shed light on what I am doing wrong?

标签: powershell
2条回答
smile是对你的礼貌
2楼-- · 2020-05-09 17:23

Building a PSCustomObject there is no need to store the values in a variable first. From your code it is not clear where you get some vars from.

$computerName ='servername'

$object=[PSCustomObject]@{
    ComputerName              = $computerrealname
    ComputerType              = $Computertype
    OSType                    = (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computerName -ErrorAction SilentlyContinue ).Caption  
    TotalMemory_GB            = [Math]::Round(((Get-WMIObject -class Win32_PhysicalMemory -ComputerName $computerName | Measure-Object Capacity -Sum).Sum / 1GB),2)
    NumberofProcessors        = (Get-WmiObject -Class Win32_processor -ComputerName $computerName).Numberofcores
    NumberofLogicalProcessors = (Get-WmiObject -Class Win32_processor -ComputerName $computerName).NumberofLogicalProcessors
    NumberofSockets           = (Get-WmiObject -Class win32_processor -ComputerName $computerName | Measure-Object socketdesignation).Count
    MachineType               = (Get-WmiObject win32_computersystem -ComputerName $computerName).Model
}
查看更多
家丑人穷心不美
3楼-- · 2020-05-09 17:33

You just need to expand the one property you would like to include. For example:

$object | Add-Member -MemberType NoteProperty -name NumberofProcessors -Value $PhysicalCores.Sum

The issue is that $PhysicalCores is some object with one or more key=value pairs, which are represented as @{key=value}.

You could also do this during assignment, for example:

$PhysicalCores = Get-WmiObject   -Class Win32_processor -ComputerName $computerName | Measure-Object -Property Numberofcores -Sum | select -expandproperty Sum

or

$PhysicalCores = (Get-WmiObject   -Class Win32_processor -ComputerName $computerName | Measure-Object -Property Numberofcores -Sum).Sum
查看更多
登录 后发表回答