Introduction to PowerShell and its programmatic access

Source: Internet
Author: User

PowerShell is a next-generation command-line shell that is more powerful and easier to use than its predecessor (Cmd.exe). The fundamental difference is that it is an object-based operation (based on the. NET Framework) rather than a string-based operation.

This tool can be used alone and can completely replace Cmd.exe. For example:

But it's much more than that, for example, we can easily get all the process names

Let's take another look at the following example to get a list of currently running services. (can be easily filtered with conditions)

In addition, PowerShell supports customization, such as Microsoft's many products provide dedicated PowerShell plug-ins (typically: SQL Server,sharepoint server, Exchange server, etc.)

The management of the server can be achieved through these special enclosures. Features are very powerful. For example, the following sqlps can view a database like a folder

Another example of EMS (Exchange managment Shell), you can modify an address list

It looks pretty good, for more details about PowerShell, if you're interested, you can refer to Microsoft's documentation.

Next talk about another topic, PowerShell is so powerful, but after all it is manual operation, can you call it in the program, and perform the relevant operation?

The answer is: yes.

Let's look at a small example

Add a reference. This assembly is in the C:\Program Files (x86) \reference assemblies\microsoft\windowspowershell\v1.0 Directory

Write the following simple code

Using System;Using System.Collections.Generic;using System.Linq; using System.Text; using System.Management.Automation; using System.Management.Automation.Runspaces; using System.Diagnostics; namespace powershellinvoker{ class Program { static void Main (string[] args) {var runspace = Runspace Factory.createrunspace (); Runspace. Open (); var piple = runspace. Createpipeline ("get-process"); var result = Piple. Invoke (). Select (p = = p.baseobject). Cast<process> (); foreach (var item in result) {Console.WriteLine ("{0}\t{1}\t{2}", item. Id.tostring (). PadRight (+), item. Processname.padright (+), item. Threads.count); } console.read (); } }}

Yes, PowerShell is a. NET framework-based object operation

Introduction to PowerShell and its programmatic access

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.