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: Continue reading “Import PowerShell Classes from modules”
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. …Continue Reading
I have enabled few site related changes which you may find more useful:
- Instead of showing full blog post at the home page, there will be a detailed excerpt of few lines and it will be followed by a shortcut leading to full blog post. Over the time, the blog has grown too large to show everything on a single page. So you don’t have to keep scrolling to see post of your interest.
- If you open a blog post, there is now related content at bottom of the post. This will help in finding related content more easily on this blog.
Let me know your thoughts on same. I would love to hear from you.
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 one …Continue Reading
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. …Continue Reading
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. …Continue Reading