Chapter 21. Part 1 — Create objects and output objects. PowerShell in depth Don Jones, Richard Siddaway

This Chapter gives an understanding of the "spirit" of PowerShell, its ideology in the field of data transfer on the conveyor, and is essential to understanding powerscaling style of programming.

Chapter 21. Creating objects for output

this Chapter contains:
"objectification" your output
create a custom object (contains information how to create objects in the PoSh)
Working with collections of properties


In previous chapters we showed You how to create a simple script and turn it into a function. We emphasize that the scripts and functions should output one and only one data type, in our previous simple example we used only one command writes to the output only one data. But you will certainly be in the situation when you need to execute some commands, combining their output and give this combination to the output of the function or script. This Chapter will show you that the main purpose of creating objects (custom objects) is integrating data from multiple objects into one object, and subsequent withdrawal from a script or function. Richard recalls how at a conference a question was asked whether the PowerShell command running like Union of SQL. In this Chapter you will get to know PowerShell because you will be working with objects.

21.1 Why output objects?
PowerShell produces only objects. Objects are the only things that can get in the way of a script (or function, at this point all that applies to the script can be applied to functions). You may have the need to display only a simple Boolean value (boolean), but it is also an object. The date object. A string or a symbol object. More complex data such as the details of the processor or service, they are all represented as objects.

retreat: We still see a lot of people write the output strings instead of full objects from their scripts (approx interpreter — ie concatenate the data into a string separated by commas, etc.). Don't do it. You should always output to output an object. If you still don't know how ask a question on the forum — ask them to tell about the display of objects.

paste from the translator — Style pose different from classic programming language, posh is the language of administration, automation, and control "big blocks". You need is committed to maximum simplicity and understandability. Before you take a text output of the command was being parsed it, and it was right. In poshe you make an object and operate the object. The difference is huge.

Example — you need to look at NetBIOS and analyze some data. Below posh pics:
the
# do the function
Function Get-NBTName
{
# get the console output of the NBTSTAT command, immediately throw out the junk
$data=nbtstat /n | Select-String "<" | where {$_ -notmatch "__MSBROWSE__"}

# cut each line of garbage
$lines=$data | foreach { $_.Line.Trim() }

# split each string into an array of elements.
# the resulting hash is placed in the table forming the object
$lines | foreach { $temp=$_ -split "\s+"
[PSCustomObject]@{
Name=$temp[0]
NbtCode=$temp[1]
Type=$temp[2]
Status=$temp[3]
}
}
}


now we make a function call, and after autoformation sortable such as:
the
PS C:\> Get-NBTName | sort type | Format-Table –Autosize


the output is:
the
Name Type Status NbtCode
---- ------- ---- ------
MYCOMPANY <1E> GROUP Registered
MYCOMPANY <00> GROUP Registered
MYCOMPANY <1D> UNIQUE is Registered
CLIENT2 <00> UNIQUE is Registered
CLIENT2 <20> UNIQUE is Registered

Total output of function Get-NBTName objects which you can transfer, sort, sample, etc. for Any%%.

One could argue — so what, in the beginning after all the parsing was. The answer is in police you parsite 1 times — at the input tool from an outstanding line, and the command line parsite you always, to the output of the input of each function. You will have to do the parsing every time when you get the output from nbtstat and then pass it to ping then try to do a tracert for example and each time you will have to sort strings.

Objects are just data structures that PowerShell understands and with which he can work. Developers say it is not necessary, and we will not explain it.
The custom object allows to follow the main principle — the script or the function should issue only one kind of objects, for example, formed on the basis of several different WMI calls. When you need to display information that is received from multiple sources, you need to create an object in which to place this information. Let's take a script designed for reporting purposes only, assuming you do not want to do inside and want only to collect the data and pass it on.
We use four commands shown in listing 21.1. Each retrieves data on the computer (restrict localhost, but can work on any computer).

tip: if you create a function using the computer name as a parameter, use $Env:COMPUTERNAME by default instead of localhost or ".". There are times when you want the machine name, which you can obtain from the environment variable and save extra steps in your code.

You don't want to display all this information, you only need a portion from each of the four terminals. In listing 21.1, we get data and put into variables, any output is not performed. It is a static piece of code that we will use on creating objects in different ways.

Listing 21.1 the Initial team.
the
$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 last of the four teams is a little different. The first three is an operating system, a computer system, and the BIOS, they exist by definition, in one instance, the processor can often be more than one. Since all processors are identical, we make the sample Select –First 1. Windows Server 2003 and Windows XP will return one instance of class Win32_Processor to the kernel, so it should be remembered that the results of the use of this class will vary depending on operating system version.

note. In hotfix a hotfix is available for this problem for Windows Server 2003 on the support.microsoft.com/kb/932370 given the limited time remaining in the life cycle of this product might be not appropriate to put it.

Thus, each of our four variables contains a single object. It is important for the following equipment which we consider.
Council. In PowerShell 3 and 4 you have the opportunity to use Common information model (CIM) cmdlets cmdlets instead of WMI. In the example in this Chapter is not important how you get data via WMI or CIM.

Insert translator
PowerShell without WMI is not complete, the full power of Pasika will be revealed if to use WMI (or CIM) in conjunction with remote execution of commands. The note Siddaway if the admin knows posh, but the WMI does not know he loses up to 60% of the power of language. Don't know how it is calculated.

So, the variables are filled, we are ready to insert them into our code.

continued
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

March Habrameeting in Kiev

PostgreSQL load testing using JMeter, Yandex.Tank and Overload

Monitoring PostgreSQL with Zabbix