powersharp: dotnet core C# powershell library
Introduction
powersharp is a dotnet core c# library which has the capability to run powershell scripts and process the resulting objects.
powersharp supports 3 different types of runspaces:
- Local sessions
- Remote Sessions: Import or Enter session modes
The library runs fine on major operating systems:
- Windows10
- Linux Debian 10
- MacOS X Catalina
Let's have a look at a sample code:
Get-Process | Get-Member
Handles AliasProperty Handles = Handlecount
Name AliasProperty Name = ProcessName
NPM AliasProperty NPM = NonpagedSystemMemorySize64
PM AliasProperty PM = PagedMemorySize64
SI AliasProperty SI = SessionId
VM AliasProperty VM = VirtualMemorySize64
...
<<<<<<< HEAD Let's then get 2 fields, Name and VM
Get-Process | Select-Object -Property ProcessName, VM
=======
Let's then get 2 fields, **Name** and **Id**
```powershell
Get-Process | Select-Object -Property Id, Name
>>>>>>> c40f1ec... powersharp 1/xxx
class Process
{
[PSMember("Id", typeof(int))]
public int Id {get; set; }
[PSMember("Name", typeof(string))]
public String Name {get; set; }
}
var shell = new LocalShellInfo();
var initializer = new LocalPSSessionInitializer(shell);
PSProcessor<Process> processor = new PSProcessor<Process>(process =>
{
Console.WriteLine($"{process.Id}: {process.Name}");
});
using (var powershell = new PowershellRunner(initializer))
{
try
{
await powershell.run("Get-Process", processor);
}
catch (Exception ex)
{
....
}
}
The output:
4001 bash
4957 bash
7620 bash
9321 bash
11630 bash
14585 bash
....
This sample outputs on console, but we could imagine to send data to a queue system for logging purpose so that user could make some analytics.
Here I show a sample to run local PowerShell commands, but the library allows user to connect to a remote office365 endpoint to perform scripts over Exchange, Sharepoint, Teams, ....
I plan to release this library as open source in the coming weeks under MIT license and will be available through a nuget repository.
Stay tuned !