This blog post is continuation of series of posts on understanding classes in PowerShell. Part 06 of series can be read at here. If you have defined PowerShell class inside a module, its not easy to import class in the current session. By default, it does not load classes. Let’s understand the details of it.
Import-Module does not import Classes
Import-Module has been a standard way of importing functions defined in a PowerShell module. It also comes with -verbose parameter so that you can see what has been imported. For understanding the case with classes, let’s define a simple PowerShell module HelloModule.psm1 as below code:Read More »
This blog post is continuation of series of posts on understanding classes in PowerShell. Part 05 of series can be read at here. In previous blog post, we have learned about meaning of inheritance, define child class and parent class, overload methods, override methods of base class and calling base class members anyhow.
Effect of inheritance on constructors
When we discussed about inheritance, we said that all properties and methods of a base class are inherited. However, constructors (check here on what are constructors and how to define them), are not inherited by a child class. Instead, like all PowerShell classes, the new class is created with a default (parameter-less) constructor. If we think about it, this makes sense after all. The purpose of a constructor is to initialize the object associated with a class. Read More »
This blog post is continuation of series of posts on understanding classes in PowerShell. Part 04 of series can be read at here.
What is Inheritance
In many classes that you create, you will find that you often need the same properties and methods from another class that you created earlier. For example, if you have a base class called the Person class, and it contains LastName and FirstName properties and a Print method, you will find that for an Employee class, you need the same properties and methods. You may also need additional properties, such as EmployeeID and Salary. When you inherit from the Person class (the base class) you can add these properties to this new Employee class, and still have access to all of the properties in the Person class. Inheritance is about the ability for one class to define itself as having all the properties and methods of a particular class, and then extending the definition of the base class by adding additional properties and methods.
Why should we use Inheritance
Inheritance is desirable because you want to avoid writing the same code over and over again. If you have two separate classes, and each oneRead More »
If we need to know, which PowerShell host version is being used, we can use
(Get-Host).name in PowerShell:
PS C:\Windows> (get-host).name
This blog post is continuation of series of posts on understanding classes in PowerShell. Part 03 of series can be read at here.
Adding Methods to PowerShell Classes
What are methods
Till now, we have discussed how to add or view properties for the classes and create instances of the classes. However, the objects which are representation of real-world objects, do not only have properties, they also do have actions associated with themselves. You can think of methods as representation of actions associated with an object. For example, a dog barks, chew, bite etc. or a car is driven, parked, accelerate etc. Alternatively, you can also think of it as a fancy way of defining functions inside a class. However, you would limit yourselves to a function which is appropriate to that class only.
A method is defined in the same way as a function and it can take arguments in the same as a function. Also, all methods must return a value.Read More »
This blog post is continuation of series of posts on understanding classes in PowerShell. Part 02 of series can be read at here.
Adding Enums to PowerShell Classes
What are Enums
Sometimes, it makes sense to use
Enums. After all, there can be so many variations of a particular property and coding them in advance helps in automatic parameter validation. When applied as a type constraint for a property in a Windows PowerShell class, an
enum will generate an error if the value supplied is not a valid enumeration.
For example, let’s consider a property called color. If we stick to basic color such as red or blue, it is great. We can limit our search by constraint such as red and it will return all matching results. But if we use Scarlet, imperial red, indian red, spanish red, rusty red, ruby, chili red, redwood and so on, it can get messy quickly. Read More »
This blog post is continuation of series of posts on understanding classes in PowerShell. Part 01 of series can be read at here.
What is an Constructor
Constructor is an type of method or function which is called only when the object is created. It is never called after that no matter how many times you called the method or function.
The most common use of the constructor is to initialize properties related to the Object. As in .NET, the constructor must use the same name as the class.
For example, building on our previous example of class Dog, we can build a constructor as:Read More »
Starting in Windows PowerShell 5.0, Windows PowerShell adds language for defining classes and other user-defined types. It is achieved by using the similar syntax and semantics being in use by other major object oriented programming languages. The goal is to enable developers (which I know, hate default syntax format and looking to build on their existing knowledge) and IT Professionals (to use more of their knowledge and make use of more formal coding mechanisms to achieve better code) to embrace Windows PowerShell for a wider variety of use cases. The use cases can be Desired State Configuration (DSC) resources, Generate and Handle exceptions, Introducing custom types, refactoring and re-using same code etc. This blog post is intended to cover PowerShell classes and their implementation in detail. This will be spread over multiple posts as this is a huge topic.Read More »