First Knowledge POWERSHELGL 5.0 class

Source: Internet
Author: User

In the past, beans in PowerShell. Processing custom objects is generally converted from a hash table. Today looked at the PS 5 inside the class function, found that this function is too good, if there are other object-oriented development language Foundation, it is easy to get started.


Just take a look at the example.


For example, I create a people class.


Note The main points:

    1. Format

    2. I specified a static property through static $sex

    3. Enum is another very NB function of PS5, you can create an enumeration class yourself, and then an instance of this class can only appear within a limited scope; For example, nationality (nationality) can only be the names of the few countries I specify.


Class people{[string] $name [int] $agestatic [string] $sex = ' Male ' [string] $career [nationality] $nationality}enum nationality{China = 1 Japan = 2 Australia = 3 USA = 4 Russia = 5}


To create a good class, we need to instantiate it. Here are 2 ways to instantiate, the first is the same as the traditional way, new-object implementation, and then each instance of the property assignment


$obj =new-object people$obj.name= ' Alex ' $obj. age=20$obj.career= ' IT ' $obj. nationality= ' China ' $obj:: sex$obj


The result is as follows, noting that the static members of his class can only be displayed by:: Instead of being displayed in the properties of the instance object

Malename age Career nationality------------------------Alex



The second instantiation is instantiated by using the new () constructor, and the first difference is that the first method can also specify the property by-property at the same time as the initialization, but the new () default constructor can only assign a value to the property by =

$obj 2=[people]::new () $obj 2.name= ' James ' $obj 2$obj3=new-object people-property @{' name ' = ' Kevin '; ' Age ' = 30; ' Career ' = ' Chef '; ' Nationality ' = ' Japan ' $obj 3



If you must assign a value together when new () is instantiated, we can manually override a constructor

Like what

Class people{[string] $name [int] $agestatic [string] $sex = ' Male ' [string] $career [Nationality] $nationalitypeople ($ Name, $age, $career, $nationalty) {$this. name= $name $this.age= $age $this.career= $career $this.nationality= $nationalty }}enum nationality{China = 1 Japan = 2 Australia = 3 USA = 4 Russia = 5} $user =[people]::new (' Zhangsan ', ' receptionist ',       "China") $user-------name age Career nationality------------------------Zhangsan Receptionist China



Then simply look at the inheritance of the class


Inheritance relationship through: implementation.


For example, I create a subclass immigrant, inherit people, a subclass can define a new attribute

Class Immigrant:people{[nationality] $destination} $immi =new-object immigrant-property @{' name ' = ' qq '; ' Age ' = 30; ' Career ' = ' dancer '; ' Nationality ' = ' Japan '; ' Destination ' = ' USA '} $immi------------PS c:\windows\system32> $immidestination: Usaname:qqage:30caree R:dancernationality:japan


Finally, we make a simple comparison between the process-oriented and object-oriented.


The first is the traditional way, through the hash table to the custom object, this kind of development thought should be the process-oriented

Function get-systeminfo{[cmdletbinding ()]param ([string[]] $ComputerName) Begin{}process{[email protected] () foreach ($computer in $ComputerName) {try{write-verbose "querying OS and Computer System" $os =get-wmiobject-class win32_operatingsystem-erroraction Stop $cs =get-wmiobject-class win32_computersystem-erroraction stop}catch{$computer |out-file c:\temp\error.txt-append}[ email protected]{computername= $computer; Lastboottime= $os. Converttodatetime ($os. LastBootUpTime); osversion= $os. Version; Manufacture= $cs. manufacturer; Model= $cs. Model} $obj =new-object-typename psobject-property $prop $obj.psobject.typenames.insert (0, ' Yuan.systeminfo ') Write-output $obj}}end {}}get-systeminfo-computername sydav01


Then I wrote the same function with object-oriented thinking, creating classes, encapsulating fields and methods inside classes, and instantiating

Class Sysinfo{[string] $model [string] $computername [string] $manufacture [string] $lastboottime [string] $ Osversiongetinfo ($computername) {$os =get-wmiobject-computername $computername-class Win32_OperatingSystem- ErrorAction Stop $cs =get-wmiobject-computername $computername-class win32_computersystem-erroraction Stop$this. Lastboottime= $os. Converttodatetime ($os. LastBootUpTime); $this. Osversion= $os. Version; $this. Manufacture= $cs. manufacturer; $this. Model= $cs. Model}} $obj =new-object sysinfo $obj. Computername= ' sydav01 ' $obj. GetInfo ($obj. ComputerName) $obj


They all have the same results.

Model:vmware7,1computername:sydav01manufacture:vmware, inc.lastboottime:7/11/2016 10:03:56 AMOSVersion : 6.3.9600


The beans are simply simple to understand the basic use of PowerShell 5 class. The three main features of the class, encapsulation, inheritance, and polymorphism, Powershell 5 specifically realize which features still need to be tried slowly.

This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1878424

First Knowledge POWERSHELGL 5.0 class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.