Introduction to PowerShell and its programmatic access

Source: Internet
Author: User
Keywords Can for example this very
Tags access customization example get list microsoft names powershell

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

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

To take another look, here's an example of getting a list of services that are currently running. (Can be easily filtered by conditions):

In addition, PowerShell also supports customization, for example, many of Microsoft's products offer specialized PowerShell plug-ins (typically: SQL Server,sharepoint server, Exchange server, and so on). Through these special shell, can realize the management of the server. function is very powerful. For example, the following sqlps can view a database like a folder:

For example, the EMS (Exchange Managment Shell) in the following illustration can modify an address list:

Looks good, for more details on PowerShell, if you are interested, you can refer to Microsoft's documentation. Next to talk about another topic, PowerShell is so powerful, but after all, manual operation, can you call it in the program, and perform the relevant operations?

The answer is: yes. Here's 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 = Runspacefactory.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 (30),
Item. Processname.padright (30),
Item. Threads.count);


}

Console.read ();
}
}
}

Yes, PowerShell is based on the. NET Framework's object operations.

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.