0

My goal is to save some info into a variable that later will be written out ($Server). I have created a cmdlet that gets some info from the CPU, which gives me the result i want to add into my server variable. When i add it i get a $null value.

PS C:\> $CPU = Get-CPUInfo -Computername .

PS C:\> $CPU

CPUSpecification                         CPUCurrentClockSpeed CPUMaxClockSpeed Socket                                                                   
----------------                         -------------------- ---------------- ------                                                                   
Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz                 2300             2400 {@{SocketDesignation=U3E1; NumberOfCores=2; NumberOfLogicalProcessors=4}}



PS C:\> $Server = New-Object PSObject

PS C:\> Add-Member -InputObject $Server -MemberType NoteProperty -Name "CPU" -Value $CPU

PS C:\> $Server

CPU                                                                                                                                            
---                                                                                                                                            
{$null, @{CPUSpecification=Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz; CPUCurrentClockSpeed=2300; CPUMaxClockSpeed=2400; Socket=System.Object[]}}



PS C:\> 

I would like to know why i get this $null inside, and how do i change it if it is some usefull information?

Later on i will create a .json file that stores my $Server variable, and it also saves the null value inside there.

I really do not like the idea of my users recieving null values where none are expected.

EDIT: I have made this custom Get-CPUInfo cmdlet that i use, however it gives me what i need and no $null value:

function Get-CPUInfo
{
    [CmdletBinding()]
    Param
    (
        # Name of the computer to get version from
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias("CN", "Host", "HostName")]
        [string]$Computername
    )
    $MySocket = Get-WmiObject -Class Win32_Processor -ComputerName $ComputerName
    $IsSocketArray = $MySocket -is [system.array]

    $SocketInfo
    $SocketInfoArray = @()
    if(-not $IsSocketarray)
    {
        $SocketInfo = New-Object PSObject
        Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "SocketDesignation" -Value $MySocket.SocketDesignation
        Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "NumberOfCores" -Value $MySocket.NumberOfCores
        Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "NumberOfLogicalProcessors" -Value $MySocket.NumberOfLogicalProcessors

        $SocketInfoArray += $SocketInfo
    }
    else
    {
        foreach ($s in $MySocket)
        {
            $SocketInfo = New-Object PSObject
            Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "SocketDesignation" -Value $s.SocketDesignation
            Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "NumberOfCores" -Value $s.NumberOfCores
            Add-Member -InputObject $SocketInfo -MemberType NoteProperty -Name "NumberOfLogicalProcessors" -Value $s.NumberOfLogicalProcessors

            $SocketInfoArray += $SocketInfo
        }
    }

    $CPUSpecification = if($IsSocketarray){ $MySocket[0].Name} else{ $MySocket.Name}
    $CPUCurrentClockSpeed = if($IsSocketarray){ $MySocket[0].CurrentClockSpeed} else{ $MySocket.CurrentClockSpeed}
    $CPUMaxClockSpeed = if($IsSocketarray){ $MySocket[0].MaxClockSpeed} else{ $MySocket.MaxClockSpeed}

    $CPUInfo = New-Object PSObject
    Add-Member -InputObject $CPUInfo -MemberType NoteProperty -Name "CPUSpecification" -Value $CPUSpecification
    Add-Member -InputObject $CPUInfo -MemberType NoteProperty -Name "CPUCurrentClockSpeed" -Value $CPUCurrentClockSpeed
    Add-Member -InputObject $CPUInfo -MemberType NoteProperty -Name "CPUMaxClockSpeed" -Value $CPUMaxClockSpeed
    Add-Member -InputObject $CPUInfo -MemberType NoteProperty -Name "Socket" -Value $SocketInfoArray

    return $CPUInfo
}
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Mike
  • 850
  • 10
  • 33
  • @wOxxOm i tried your line. However it still gives me the $null value: {$null, @{CPUSpecification=Intel... etc. – Mike Feb 21 '17 at 13:24
  • @wOxxOm i added the Get-CPUInfo. However it does give me the info i need and no $null values as shown in the first code example. – Mike Feb 21 '17 at 13:41
  • 2
    @MikeHjortChristensen Remove the bare `$SocketInfo` line from `Get-CPUInfo` - that's the one emitting as `$null` – Mathias R. Jessen Feb 21 '17 at 13:46

1 Answers1

1

PowerShell doesn't print null elements of an array when you display it directly, but it'll be printed in a compact representation of properties.

  • Let's check:

    (Get-CPUInfo -Computername .).GetType()
    

    Object[] System.Array

    (Get-CPUInfo -Computername .).Count
    

    2

    (Get-CPUInfo -Computername .)[0] -eq $null
    

    True

  • Something outputs a null value in the Get-CPUInfo function:

    $SocketInfo
    

    Remove it.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136