How to create objects in PowerShell
Found that search engines Runet issued mostly obsolete methods such as add-member. How to create objects, why so and why is it necessary in the translation from the PowerShell guru. You will learn the technique of creating object from hash table and learn a few tricks for working with them.
PowerShell in Depth
Chapter 21.1 Technique number 1. The use of hash tables to create custom objects.
Let's start of reception which usually we use ourselves when we want to create your own object or to merge the information from different objects into one for subsequent withdrawal. We call this the official way or recommended. We use it because it makes it easy to write code that reads well, and ultimately allows you to make faster my work.
This method is demonstrated in listing 21.2 below
the
by performing this code you will get the result like this:
the
Because the output you have more than four properties PowerShell did display in the list view. You could do
the
to get a table. Please note, the fact that you create one object by combining information from four sites. You do this by creating a hash table in which prescribed the desired property names, values are property values of other objects. This is what you did in the hash table pointing:
the
If you place entries in the hash table on one line, you will need to separate each property with a semicolon. If you put each property on a separate line, you don't need a semicolon, you will much easier to read and edit code.
Brackets with presutstvuet familiar to them @ they say that then begins the hash table. Because the table is in the variable $props it is easy to pass in parameters to the property of the new object. Object PSObject is specifically provided for this purpose.
The advantage of this approach is that it is easy to build a hash table on the fly and create a lot of custom objects. You may notice that in the output object properties are not the same order as they were defined in the table. One possible solution is to create the formatting for the custom object (special XML file describing how to display, in what order, etc.) or if you use powershell 3 or higher, you can use the property ordered
the
Everything else is the same. Now the object properties are displayed in the order they were written. If you pass $obj Get-Member, you will see that it is PS-CustomObject.
Note PowerShell by default does not track the order of elements in the hash table. That's why when you see the final conclusion of its properties are not in the order in which you created them. Starting with PowerShell 3, you can fix this by using the attribute [ordered]. This creates an ordered dictionary (also called hash table) and supports the order of the elements in it.
I will add.
This method is very convenient: easy to read, easy to edit. This method is widely used in PowerShell from settings to send mail via SMTP, to merge into a single object information to display from the function or script.
Article based on information from habrahabr.ru
PowerShell in Depth
Chapter 21.1 Technique number 1. The use of hash tables to create custom objects.
Let's start of reception which usually we use ourselves when we want to create your own object or to merge the information from different objects into one for subsequent withdrawal. We call this the official way or recommended. We use it because it makes it easy to write code that reads well, and ultimately allows you to make faster my work.
This method is demonstrated in listing 21.2 below
the
# Collect kakieto data, in the future we need to build and bring
$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
# The hash table. Describes the properties of the future project
$props = @{OSVersion=$os.version
Model=$cs.model
Manufacturer=$cs.manufacturer
BIOSSerial=$bios.serialnumber
ComputerName=$os.CSName
OSArchitecture=$os.osarchitecture
ProcArchitecture=$proc.addresswidth}
# Create an object from the hash table and print
$obj = New-Object –TypeName PSObject –Property $props
Write-Output $obj
by performing this code you will get the result like this:
the
Manufacturer : Microsoft Corporation
OSVersion : 6.3.9600
OSArchitecture : 64-bit
BIOSSerial : 036685734653
ComputerName : RSSURFACEPRO2
Model : Surface Pro 2
ProcArchitecture : 64
Because the output you have more than four properties PowerShell did display in the list view. You could do
the
Write-Output $obj | ft
to get a table. Please note, the fact that you create one object by combining information from four sites. You do this by creating a hash table in which prescribed the desired property names, values are property values of other objects. This is what you did in the hash table pointing:
the
Manufacturer=$cs.manufacturer
If you place entries in the hash table on one line, you will need to separate each property with a semicolon. If you put each property on a separate line, you don't need a semicolon, you will much easier to read and edit code.
Brackets with presutstvuet familiar to them @ they say that then begins the hash table. Because the table is in the variable $props it is easy to pass in parameters to the property of the new object. Object PSObject is specifically provided for this purpose.
The advantage of this approach is that it is easy to build a hash table on the fly and create a lot of custom objects. You may notice that in the output object properties are not the same order as they were defined in the table. One possible solution is to create the formatting for the custom object (special XML file describing how to display, in what order, etc.) or if you use powershell 3 or higher, you can use the property ordered
the
$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 object properties are displayed in the order they were written. If you pass $obj Get-Member, you will see that it is PS-CustomObject.
Note PowerShell by default does not track the order of elements in the hash table. That's why when you see the final conclusion of its properties are not in the order in which you created them. Starting with PowerShell 3, you can fix this by using the attribute [ordered]. This creates an ordered dictionary (also called hash table) and supports the order of the elements in it.
I will add.
This method is very convenient: easy to read, easy to edit. This method is widely used in PowerShell from settings to send mail via SMTP, to merge into a single object information to display from the function or script.
Комментарии
Отправить комментарий