Does PowerShell support OOP?

What are concepts like Class, Interface, Mixin in PowerShell? Does it support OOP? If so, where can I read about it?

+6
oop powershell
source share
4 answers

You can define new types in PowerShell v2.0 using the Add-Type cmdlet:

DETAILED DESCRIPTION

The Add-Type cmdlet allows you to define a .NET class in a Windows PowerShell session. Then you can create objects (using the New-Object cmdlet) and use the objects, just like you would use any .NET ob object. If you add the Add-Type command to your Windows PowerShell profile, the class will be available in all Windows PowerShell sessions.

You can specify the type by specifying existing assembly or source code files, or specify the source code in a string or save in a variable. You can even specify only a method, and Add-Type will define and generate a class. You can use this function to invoke Invoke (P / Invoke) platform calls for unmanaged functions in Windows PowerShell. If you specify the source code, Add-Type compiles the specified source co de and creates an assembly in memory that contains the new .NET types.

You can use Add-Type parameters to specify an alternative language and compiler (CSharp by default), compiler parameters, assembly dependencies, class namespace and type names, and the resulting assembly.

help Add-Type for more information.

Also see:

+9
source share

PowerShell is more of an OOP user language. It can use most of the .NET Framework, but it does not support the creation of interfaces, classes, and, of course, not the mixins..NET, on which a system like PowerShell is based, does not support mixins. PowerShell supports the dynamic addition of properties and methods to an existing object using the Add-Member cmdlet.

Add-Type is useful, but if you need to escape to C # or VB to define a class or class that implements a specific interface, I would not consider that the first class supports the creation of classes / interfaces.

If you're looking for free training material, check out Powerful Windows PowerShell .

+7
source share

The PowerShell pipeline deals with objects, not just the text stream that the Unix pipeline does. All variables are instances of objects. These are all .NET, BTW objects.

Here, part of the output of the ls command is passed to the get-member cmdlet:

  PS C:\Documents and Settings\Administrator.DEV-3DPST1-SWK> ls | get-member TypeName: System.IO.DirectoryInfo Name MemberType Definition ---- ---------- ---------- Create Method System.Void Create(DirectorySecurity directorySecurity), System.Void Create() CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(Type requestedType) CreateSubdirectory Method System.IO.DirectoryInfo CreateSubdirectory(String path), System.IO.Director... Delete Method System.Void Delete(), System.Void Delete(Boolean recursive) Equals Method System.Boolean Equals(Object obj) GetAccessControl Method System.Security.AccessControl.DirectorySecurity GetAccessControl(), System.... GetDirectories Method System.IO.DirectoryInfo[] GetDirectories(String searchPattern), System.IO.D... GetFiles Method System.IO.FileInfo[] GetFiles(String searchPattern), System.IO.FileInfo[] G... GetFileSystemInfos Method System.IO.FileSystemInfo[] GetFileSystemInfos(String searchPattern), System... GetHashCode Method System.Int32 GetHashCode() GetLifetimeService Method System.Object GetLifetimeService() GetObjectData Method System.Void GetObjectData(SerializationInfo info, StreamingContext context) GetType Method System.Type GetType() get_Attributes Method System.IO.FileAttributes get_Attributes() get_CreationTime Method System.DateTime get_CreationTime() 

get-member displays the elements of the object you are connecting to. You can see that these are real members of the System.IO.DirectoryInfo class.

0
source share

Powershell version 5 seems to support some of the major OOPs.

All credit belongs to this guy: https://xainey.imtqy.com/2016/powershell-classes-and-concepts/

Class Example:

  class myColor { [String] $Color [String] $Hex myColor([String] $Color, [String] $Hex) { $this.Color = $Color $this.Hex = $Hex } [String] ToString() { return $this.Color + ":" + $this.Hex } } 

An example of an abstract class:

 class Foo { Foo () { $type = $this.GetType() if ($type -eq [Foo]) { throw("Class $type must be inherited") } } [string] SayHello() { throw("Must Override Method") } } class Bar : Foo { Bar () { } [string] SayHello() { return "Hello" } } 
-one
source share

All Articles