Found that runet search engines issue mostly outdated
add-member methods. How to create objects, why this is the case and why it is necessary in translation from the gurus of PowerShell. You will learn how to create an object from a hash table, and learn a few tricks to work with them.
PowerShell in Depth
Chapter 21.1 Technique number 1. Using hash tables to create custom objects.')
Let's start the techniques that we usually use ourselves when we need to create our own object or merge information from different objects into one for subsequent output. We call this path official, or recommended. We use it because it makes it easy to write code, we read well and ultimately allows us to do our work faster.
This method is shown in Listing 21.2 below.
# , $os = Get-WmiObject –Class Win32_OperatingSystem –comp localhost $cs = Get-WmiObject –Class Win32_ComputerSystem –comp localhost $bios = Get-WmiObject –Class Win32_BIOS –comp localhost $proc = Get-WmiObject –Class Win32_Processor –comp localhost | Select –First 1 # . $props = @{OSVersion=$os.version Model=$cs.model Manufacturer=$cs.manufacturer BIOSSerial=$bios.serialnumber ComputerName=$os.CSName OSArchitecture=$os.osarchitecture ProcArchitecture=$proc.addresswidth} # $obj = New-Object –TypeName PSObject –Property $props Write-Output $obj
By executing this code, you will get a result similar to this:
Manufacturer : Microsoft Corporation OSVersion : 6.3.9600 OSArchitecture : 64-bit BIOSSerial : 036685734653 ComputerName : RSSURFACEPRO2 Model : Surface Pro 2 ProcArchitecture : 64
Since at the output, you have an object with more than four properties. PowerShell made a list output on the screen. You could perform
Write-Output $obj | ft
to get a table. Note that the fact is that you created one object by combining information from four objects. You did this by creating a hash table in which you entered the desired property names, the values ​​for them became the property values ​​of other objects. This is what you did in the hash table pointing:
Manufacturer=$cs.manufacturer
If you put the hash table entries on one line, you will need to separate each property with a semicolon. If you put each property on a separate line, you do not need a semicolon, it is much easier for you to read and edit the code.
The parentheses with the preceding @ sign say that the hash table begins next. Since the table is in the
$ props variable
; it is easy to transfer it in the
-property parameters of the new object. The
PSObject object
is specifically designed for this purpose.
The advantage of this approach is that it is easy to build a hash table on the fly and create many user objects from it. You may notice that in the output object the properties are not of the same order as they were defined in the table. One of the possible solutions is to create formatting for a custom object (a special XML file describing how to display, in what order, etc.) or, if you use powershell 3 and higher, you can use the
ordered property
$props = [ordered]@{ OSVersion=$os.version Model=$cs.model Manufacturer=$cs.manufacturer BIOSSerial=$bios.serialnumber ComputerName=$os.CSName OSArchitecture=$os.osarchitecture ProcArchitecture=$proc.addresswidth}
Everything else is the same. Now the properties of the object will be displayed in the order in which they were written. If you pass
$ obj to
Get-Member , you will see that this is a
PS-CustomObject .
Note By default, PowerShell does not track the order of elements in a hash table. That's why when you see the final output, its properties go in the wrong order in which you created them. Starting in PowerShell 3, you can fix this by using the
[ordered] attribute. This creates an ordered dictionary (another name for the hash tables) and maintains the order of the elements in it.
I will add from myself.
Indeed, this method is very convenient: easy to read, easy to edit. This method is widely used in PowerShell - from creating parameters for sending email via SMTP, for combining information into a single object for output from a function or script.