none
Object within an Object RRS feed

  • Question

  • Hello everyone I am working on a server inventory system and have run into a snag working with custom objects in PowerShell.  Below is a bit of the code I have within my foreach statement.

    $SysObject = New-Object -TypeName System.Management.Automation.PSObject
    $SysObject | Add-Member -MemberType NoteProperty -Name Hostname -Value $Server
    $SysObject | Add-Member -MemberType NoteProperty -Name BIOSManu -Value $SysBios.Manufacturer
    $SysObject | Add-Member -MemberType NoteProperty -Name BIOSVersion -Value $SysBios.SMBIOSBIOSVersion

    It is using the standard Add-Member as a NoteProperty.  I have now come to the CPU and RAM portion and would like to individually track each CPU and RAM module attached.  So there will be a CPU0 and CPU1.  Now I could go with dynamically updating the -Name field with each CPU and tack on the different values such as Manufacturer, MaxClockSpeed and so on however I would like to make the PSObject a bit more defined.  

    I know in many commands in PowerShell you can use -ExpandProperty and have an entirely new level to the object.  Is there a way to script this into objects as well?  If so am I on the correct track?  Can anyone point me in the right direction for this type of array.

    Thank You 

    • Moved by Bill_Stewart Wednesday, September 13, 2017 9:58 PM Abandoned
    Wednesday, August 9, 2017 1:15 AM

All replies

  • First use real objects as they are easier and more flexible:

    $SysInfo = [pscustomobject]@{
    	Hostname = $Server
    	BIOSManu = $SysBios.Manufacturer
    	IOSVersion = $SysBios.SMBIOSBIOSVersion
    	Processors = @(
    		[pscustomobject]@{
    				# values for cpu 1
            	},
    		
    		[pscustomobject]@{
    			# values for cpu 2
    		},
    		[pscustomobject]@{
    			# values for cpu 3
    		}
    	)
    }
    You can also dynamically add elements to an array or an object hash.


    \_(ツ)_/



    • Edited by jrv Wednesday, August 9, 2017 1:29 AM
    Wednesday, August 9, 2017 1:29 AM
  • jrv, that is just what I needed.  Thank you very much!
    Wednesday, August 9, 2017 4:29 AM
  • How would you go about structuring the code for an undetermined number of processors.  The comma for the next object is throwing me off.  I was thinking of using a foreach ($Proc in $Processors) in there but with the comma if you add an extra you would error out the script, same if you remove it.
    Wednesday, August 9, 2017 5:00 AM
  • With foreach create each CPU/RAM as object with needed data and add them to array that is property of your(parent) object.

    Best regards,
    Pavel Volkov
    MCP, MS: Configuring Windows Devices, Hyper-V and SCVMM
    MCSA: Windows 10/Server 2012/Server 2016/SQL 2016 Database Development
    MCSE: Mobility, Cloud Platform and Infrastructure


    Please remember to mark the replies as answers if they help...

    Wednesday, August 9, 2017 5:05 AM
  • $SysInfo = [pscustomobject]@{
    	Hostname  = $Server
    	BIOSManu  = $SysBios.Manufacturer
    	IOSVersion = $SysBios.SMBIOSBIOSVersion
    	Processors = @()
    }
    
    foreach($p in $processors){
    	$SysInfo.Processors += [pscustomobject]@{
    		# values for cpu 1
    	}
    }
    

    You are going to have to use your head to scope out how this works.  It is a few very excellent techniques for doing things in PowerShell.


    \_(ツ)_/

    Wednesday, August 9, 2017 5:11 AM
  • That is perfect, thanks!
    Wednesday, August 9, 2017 5:29 AM