locked
PSCustomObject - Sorting & Adding elements RRS feed

  • Question

  • Hello fellas,

    Questions

    1. How do I modify / add a parameter inside PSCustomObject?
    2. My output isn't matching how I want it to be, I know [Ordered] exists, but it does not work when I try to set it. Any ideas?

    Here is the code.

            $computerParam = New-Object PSCustomObject -Property @{
                'hostname' = $env:COMPUTERNAME
                'serialNumber' = $compBios.SerialNumber
                'model' = $compSystem.Model
                'manufacturer' = $compSystem.Manufacturer
                'cpu' = $cpu.Name
                'os' = $compInfo.Caption
                'osVersion' = $compInfo.Version
                'osArchitecture' = $compInfo.OSArchitecture
                'ram' = $ram
                'upgradable' = 'YES'
            }

    Tuesday, February 4, 2020 7:10 PM

Answers

  • $computerParam = [pscustomobject][ordered]@{
                hostname = $env:COMPUTERNAME
                serialNumber = $compBios.SerialNumber
                model = $compSystem.Model
                manufacturer = $compSystem.Manufacturer
                cpu = $cpu.Name
                os = $compInfo.Caption
                osVersion = $compInfo.Version
                osArchitecture = $compInfo.OSArchitecture
                ram = $ram
                upgradable = 'YES'
            }
    


    \_(ツ)_/

    • Marked as answer by Nevets24 Tuesday, February 4, 2020 8:19 PM
    Tuesday, February 4, 2020 7:48 PM

All replies

  • Show the rest of the code and explain more detailed what's not working.

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, February 4, 2020 7:23 PM
  • Output will be as follow - info has been removed.

    model          :
    ram            :
    cpu            :
    serialNumber   :
    osVersion      :
    os             :
    osArchitecture :
    manufacturer   :
    hostname       :

    I'd like to add a key value to the parameter, see below.

    upgradable : 'YES' ***Goal***
    model          :
    ram            :
    cpu            :
    serialNumber   :
    osVersion      :
    os             :
    osArchitecture :
    manufacturer   :
    hostname       :

    Tuesday, February 4, 2020 7:33 PM
  • $computerParam = [pscustomobject][ordered]@{
                hostname = $env:COMPUTERNAME
                serialNumber = $compBios.SerialNumber
                model = $compSystem.Model
                manufacturer = $compSystem.Manufacturer
                cpu = $cpu.Name
                os = $compInfo.Caption
                osVersion = $compInfo.Version
                osArchitecture = $compInfo.OSArchitecture
                ram = $ram
                upgradable = 'YES'
            }
    


    \_(ツ)_/

    • Marked as answer by Nevets24 Tuesday, February 4, 2020 8:19 PM
    Tuesday, February 4, 2020 7:48 PM
  • Output will be as follow - info has been removed.

    model          :
    ram            :
    cpu            :
    serialNumber   :
    osVersion      :
    os             :
    osArchitecture :
    manufacturer   :
    hostname       :

    I'd like to add a key value to the parameter, see below.

    upgradable : 'YES' ***Goal***
    model          :
    ram            :
    cpu            :
    serialNumber   :
    osVersion      :
    os             :
    osArchitecture :
    manufacturer   :
    hostname       :

    Your question is still to vague.  You must ask a clear unambiguous question.


    \_(ツ)_/


    • Edited by jrv Tuesday, February 4, 2020 8:16 PM
    Tuesday, February 4, 2020 7:50 PM
  • Code does what it is expected to do. My output is shuffled. I am looking to have it as the order in my script block.

    Also, I am looking to add a key value inside the parameters DEPENDING on a if/else. For instance, Found the answer (Add-Member).

    • Edited by Nevets24 Tuesday, February 4, 2020 8:21 PM Answer found.
    Tuesday, February 4, 2020 8:00 PM
  • What is to vague ?

    How do I modify / add a parameter inside PSCustomObject? i think that is pretty clear.

    My output isn't matching how I want it to be, I know [Ordered] exists, but it does not work when I try to set it. Any ideas? That is vague because it's an open question. Someone asked for my output, which I provided and I added the value I am looking to add in my parameters. If that is to vague, I can't help.

    Just type it in like I did in the code I posted.  Why is that an issue?  I also showed how to make the hash "ordered".  WHy is that a problem?.  Did you even try the code?


    \_(ツ)_/

    Tuesday, February 4, 2020 8:15 PM
  • Also note that your terminology is, in part, a cause of vagueness.  You use "parameter" and "value" incorrectly.  Are you asking how to assign a value or how to create a parameter.  From your sta5temnts it is not possible to guess which one you are asking about.


    \_(ツ)_/

    Tuesday, February 4, 2020 8:19 PM
  • You were right. I edited my reply after reading it and reading the link you sent.
    Tuesday, February 4, 2020 8:22 PM