Power Shell输出大括号而不是实际值

时间:2018-08-14 17:43:05

标签: powershell

下面是我拥有的代码,该代码也可以在您的系统上重现:

$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

输出如下所示

enter image description here

如您所见,在大括号中显示某些变量的输出,如何将其删除?

我尝试过的事情
1.在不向对象添加变量的情况下,输出不会放在大括号内
2.尝试做一个sum.toint32()但没有运气

有人可以告诉我我在做什么错吗?

2 个答案:

答案 0 :(得分:2)

您只需要扩展要包括的一个属性即可。例如:

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

问题在于$PhysicalCores是具有一个或多个键=值对的对象,它们表示为@{key=value}

您也可以在分配期间执行此操作,例如:

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

$PhysicalCores = (Get-WmiObject   -Class Win32_processor -ComputerName $computerName | Measure-Object -Property Numberofcores -Sum).Sum

答案 1 :(得分:1)

构建PSCustomObject时,无需先将值存储在变量中。 从代码中还不清楚从哪里获得一些变量。

$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
}