PowerShell Introductory Tutorials Access. NET assemblies, COM, and WMI instances _powershell

Source: Internet
Author: User

PowerShell can be designed to all-inclusive, to achieve everything again, or as it is now designed to glue the language, using existing resources. In particular, the alliance with the three giants of. Net, COM and WMI makes PowerShell a great place in the Windows world.

With new Sharp. NET marriage

In the world of Windows,. NET represents the present and the future, is the Forces nouvelles. A marriage to. NET means that the table is on a thick leg and is on the fast track. Known to be based on the. Net Framework, then use. NET base class is a breeze. The basic steps are as follows: 1, add the assembly of the class, 2, create an instance of the class, 3, call the instance's function or property. Take the StringBuilder class, for example, with the following code:

Copy Code code as follows:

Add-type-assemblyname mscorlib

$str = New-object System.Text.StringBuilder

$str. Append ("Hello")

$str. ToString ()

The result is the output Hello string.

In fact, the most enjoyable thing is that PowerShell can use a custom. NET class library. This means that our products or tools can continue to work in PowerShell. The basic step is to write a class library, compile it into a DLL, import it into a PowerShell session, and then define the functions and properties that can be used. Examples are as follows:

First write the code, compiled into a DLL, assuming the path is D:\PowerShellDemo\PowerShellDemo.dll.

Copy Code code as follows:

Using System;

Namespace Powershelldemo
{
public class Powershelldemo
{
public void Greet ()
{
Console.WriteLine ("Hello!");
}
}
}

It is then imported into the PowerShell session and uses its functions or properties.

Copy Code code as follows:

Add-type–path "D:\PowerShellDemo\PowerShellDemo.dll"

$obj = New-object Powershelldemo.powershelldemo

$obj. Greet ()

The output of the above call is the Hello string.

It's so easy to use. NET's managed assembly, which is bound to attract a bunch of them.

With the nobility COM

COM like the decline of the nobility, although too angry, but that the stylish still. As a legacy, they are still glowing and feverish. PowerShell did not abandon it, but chose to be compatible with them. It is also reassuring to programmers who are still fighting in native C + +. The way PowerShell access to COM is done through its ProgID. Examples are as follows:

Copy Code code as follows:

$ie = New-object-comobject internetexplorer.application

$ie. Visible = $true

$ie. Navigate ("www.bing.com")

$ie. Quit ()

The above example demonstrates how easy it is to start, display, navigate, and exit ie. Good relationships with COM also allow PowerShell to manipulate some system interfaces to become simple and straightforward. This is definitely a bonus point design.

Allied with the powerful WMI

WMI is a powerful weapon for accessing standard resources in the native and domain, taking over WMI's resource access, and PowerShell has also entered the Giants. It does not do the extra work, but lightweight through a few WMIOBEJCT commands, it has a complete set of WMI access mechanisms. Access to other machines within the domain does not even take advantage of the PowerShell remote infrastructure. Examples of access to WMI are as follows:

Copy Code code as follows:

Get-wmiobject-namespace "root\cimv2"-class Win32_Process

The function of this command, like Get-process, is to get the processes running on this machine, except that they get a little bit of a difference in the form of the results. The real difference is that when you get process information from a remote computer, get-process must run it under the PowerShell remote infrastructure with the Invoke-command command. The Get-wmiobject command simply adds the-computername parameter. This calm, is brought from the mother of WMI.

Using WMI will greatly improve the efficiency of accessing native and remote system resources. Whether you believe it or not, I believe it anyway!

Conclusion

Whether it's based on. NET, or COM-compatible, or WMI access to resources, PowerShell to become a faceted glue, and become a public rather than a small audience of friends. As an IT person's you, have not been sticky (electricity) to?

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.