Point connection between PowerShell and. NET frameworks

Source: Internet
Author: User

One of the best features of Windows PowerShell is its direct access to the. NET object mode. Unfortunately, because I am not a developer and most programming concepts are unfamiliar to me, this is the biggest obstacle for me to learn PowerShell.

When I first started working with PowerShell, I didn't know the differences between categories, ports, constructor, or members. The concept of objects came from a programmer's point of view. But I do think these concepts are important to learning and using. NET with Windows PowerShell. I also found that learning about C # or at least understanding it) is also very helpful.

Therefore, I will try to briefly explain these concepts through examples and provide some code to help you in this process. I will also try to use my limited capabilities) to provide some basic guidance on C # PowerShell conversion.

Let's start with the definition. In addition, if you are a developer, please feel free to comment on all the things I will talk about. These definitions are my opinions on these concepts.

. NET:It is an architecture with black box code that accepts specific input and returns a value or object. Basically, Microsoft has completed all the encoding for you-you just need to call it correctly. MSDN is an invaluable resource of the. NET architecture.

Class ):Almost everything I mentioned in. NET is a category. I tend to think that a category is a model that demonstrates what the object should be and what features and functions it should have. For example, a Microsoft. Win32.RegistryKey object should have the naming feature and the GetValue method.

Member ):Each category has members, which are linked features and class functions. If you only want to see the overview of what a category must provide, this will be a good place.

Feature Property ):Feature is one of the two types of members. In short, they can be considered as attributes of objects. For example, Microsoft. Win32.RegistryKey has features such as Name, SubKeyCount, and Value Count. Therefore, each Microsoft. Win32.RegistryKey object can have these features.

Class Function Method ):A class function is one of the two class members. Like features, class functions are restricted by classes, but they are more like category functions than attributes. Microsoft. Win32.RegistryKey categories include CreateSubKey, DeleteSubkey, SetValue, and other class functions. Like features, each Microsoft. Win32.RegistryKey object can have these class functions.

Constructor ):A constructor is a class function that collects the information required to create a category instance. I prefer to define the structure as the information or object needed to make the object useful. Using System. Data. SqlClient. SqlCommand as an example, you can create an object from this class in four different ways. The objects created by each method have slightly different data.

Static field ):They are similar to features, but can be obtained without creating an object instance. In PowerShell, it is really easy to access static members. All you have to do is use

[.NET Class]::StaticField。

In the following example, the. NET category System. Math returns the PI value:

[System.Math]::PI.

Static class functions Static method): they are like class functions, but can be obtained without creating an object instance. Similar to the way you access static domains, you only need to use [. NET Class]: StaticMethods.

In the following example, Microsoft. Win32.RegistryKey returns the Microsoft. Win32.RegistryKey object for a remote machine called MyServer.

[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,”MyServer”)

Now we have covered the basic. NET Framework. Let's examine some PowerShell features in detail, such as creating categories and discovering which features and class functions can be obtained for these categories.

Create an instance of the Microsoft. NET Framework or COM Object.

In the following example, I will create a System. DirectoryServices. ActiveDirectory. DirectoryContext instance:

$Context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("DirectoryServer",$Name)

Get-Member) to access Object Features and class functions.

In the following example, all the members of the $ Context object are returned:

$Context | Get-Member

Collect all. NET Problems

The following are working. NET instances. It uses new objects, constructor and static class functions to return the Controller objects of the Active Directory domain.

function Get-DC{Param($Name,$Domain)if($Name){$Context = new-objectSystem.DirectoryServices.ActiveDirectory.DirectoryContext

("DirectoryServer",$Name)

[System.DirectoryServices.

ActiveDirectory.DomainController]

::GetDomainController($Context)

}

if($Domain)

{

$Context = new-object

System.DirectoryServices.

ActiveDirectory.DirectoryContext

("Domain",$Domain)

[System.DirectoryServices.ActiveDirectory.

DomainController]::FindAll($Context)

}

if(!$Name -and !$Domain)

{

$DCName = ([adsi]"LDAP://rootDSE")

.dnsHostname.ToString()

$Context = new-object

System.DirectoryServices.ActiveDirectory.

DirectoryContext("DirectoryServer",$DCName)

[System.DirectoryServices.ActiveDirectory.

DomainController]::GetDomainController($Context)

}

}

Related Article

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.