September 19, 2024

Exactly How To Use.NET Qualities and Approaches in PowerShell

3 min read
rb_thumb

rbs-img

In this collection, we have completely discussed.NET namespaces and courses. Now, let’s move our focus to properties and techniques.

Advanced PowerShell Techniques with the.NET Structure

Before diving in, right here’s the sample PowerShell script we have been utilizing:

Exploring.NET Properties

The sample script has numerous homes, such as $Form.Width and $Form.Height. Both Width and Height are buildings of the Type class.

To check out homes and methods, see the.NET API browser. Click a namespace to see its classes, then pick a class to see its involved aspects, such as residential or commercial properties, methods, fields, and events.

Properties are basically attributes that can be put on a things. Our example script establishes a switch’s message and background shade making use of homes.

Properties That Reference Methods

Occasionally, you may discover that homes recommendation approaches. Consider this line of code from the script:

$ Form.controls.add($ Button1).

This line associates the button things with the kind things. Unlike other buildings reviewed, it doesn’t consist entirely of a course name and a property name divided by a duration. Instead, there is a recommendation to the formerly developed type item, followed by a property name (controls), a duration, and a technique name (add). This construction is essential because the Type class does not have an Add technique. However, it does have a Controls residential property. This residential property includes the ControlCollection (Microsoft clarifies this below), where the Include technique is accessed.

Navigating Phrase Structure and Instances.

Believe it or otherwise, determining the best courses, residential or commercial properties, and approaches is the hardest component. The real syntax is rather instinctive.

When you click a building or a technique in the.NET API internet browser, it opens up a page giving phrase structure and examples, generally written in C#. Despite the fact that the examples are in C#, they work for recognizing the values required for residential or commercial properties or approaches. For instance, in Figure 1, Form.Width requires an integer value representing the type’s size in pixels.

Number 1. Assistance is available for all the properties and approaches.

Calling a Technique Straight.

We have actually covered basic methods for functioning with courses, buildings, and techniques. Allow’s present one more idea. In previous examples, I utilized the New-Object cmdlet to produce a things connected to a namespace and a course, such as:.

$ Type = New-Object System.Windows.Forms.Form.

Although this is the typical means of doing things, some classes lack a called for builder, so you can’t utilize them with the New-Object cmdlet. As an example, to inspect if a file called C: \ test.txt exists on my hard disk, I may try:.

$ File = New-Object System.IO.File.

$ File.Exists(” C: \ Temperature \ Test.txt”).

Below, System.IO is the namespace, Data is a class, and Exists is a method within the Data course. The Exists method calls for a filename (and optional path) in string style and returns True if the file exists or False if it does not.

Nonetheless, the Data class does not have the needed erector, leading to a mistake (see Number 2).

Number 2. You can not produce an object using the Documents class.

To function around this, call the method directly. We might use this command in this instance:.

[System.IO.File]:: Exists(” C: \ Temp \ Test.txt”).

Here, the namespace and the course name are confined in braces. The two colons show to PowerShell that a.NET approach is being called. Add the technique name after the two colons and provide any kind of called for parameters (in this instance, a course and filename). See what the command resembles in Figure 3.

Number 3. You can access some.NET approaches straight.

Leave a Reply

Your email address will not be published. Required fields are marked *