Vaetech.PowerShell 1.0.4

dotnet add package Vaetech.PowerShell --version 1.0.4                
NuGet\Install-Package Vaetech.PowerShell -Version 1.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Vaetech.PowerShell" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Vaetech.PowerShell --version 1.0.4                
#r "nuget: Vaetech.PowerShell, 1.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Vaetech.PowerShell as a Cake Addin
#addin nuget:?package=Vaetech.PowerShell&version=1.0.4

// Install Vaetech.PowerShell as a Cake Tool
#tool nuget:?package=Vaetech.PowerShell&version=1.0.4                

Vaetech.PowerShell

Join the chat at (https://badges.gitter.im/Vaetech-PowerShell)

Package Latest Release
Vaetech.PowerShell NuGet Badge Vaetech.PowerShell

What is Vaetech.PowerShell?

Vaetech.PowerShell is a C# library which may be used for running commands with lambda expressions and getting data from PowerShell, in .Net Framework, .Net Standard and .Net Core

License Information

MIT License

Copyright (C) 2021-2022 .NET Foundation and Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Installing via NuGet

The easiest way to install Vaetech.PowerShell is via NuGet.

In Visual Studio's Package Manager Console, enter the following command:

Install-Package Vaetech.PowerShell

Getting the Source Code

First, you'll need to clone Vaetech.PowerShell from my GitHub repository. To do this using the command-line version of Git, you'll need to issue the following command in your terminal:

git clone --recursive https://github.com/cochachyLE-Eng/Vaetech.PowerShell.git

Updating the Source Code

Occasionally you might want to update your local copy of the source code if I have made changes to Vaetech.PowerShell since you downloaded the source code in the step above. To do this using the command-line version fo Git, you'll need to issue the following commands in your terminal within the Vaetech.PowerShell directory:

git pull
git submodule update

Building

In the top-level Vaetech.PowerShell directory, there are a number of solution files; they are:

  • Vaetech.PowerShell.sln - includes projects for .NET 4.5/4.6.1/4.8, .NETStandard 2.1 as well as the unit tests.

Once you've opened the appropriate Vaetech.PowerShell solution file in Visual Studio, you can choose the Debug or Release build configuration and then build.

Both Visual Studio 2017 and Visual Studio 2019 should be able to build Vaetech.PowerShell without any issues, but older versions such as Visual Studio 2015 will require modifications to the projects in order to build correctly. It has been reported that adding NuGet package references to

Newtonsoft.Json (>= 13.0.1) System.ComponentModel (>= 4.3.0) System.ComponentModel.Annotations (>= 5.0.0) Vaetech.Data.ContentResult (>= 1.6.6) Vaetech.Runtime.Utils (>= 1.0.3)

will allow Vaetech.PowerShell to build successfully.

Using Vaetech.PowerShell

How can it be used?

At its most basic, it only takes a few lines of code to run PowerShell queries with lambda expressions.

// Gets the processes that are running on the local computer.
var getProcessResponse = PShell.GetProcess("svchost").WhereObject(c => c.StartTime > DateTime.Now.AddDays(-5)).FormatList(x => new { x.Name, x.Id, x.StartTime });
// Get Command
string command = getProcessResponse.GetCommand();

Get results with ErrorAction in custom collection

// Gets the processes running on the local computer, in custom collection.
var getProcessResponse = PShell.GetProcess(ErrorAction.SilentlyContinue,"svchost", "w3wp").SelectObject(x => new { x.Name, x.Id, x.StartTime }).WhereObject(c => c.StartTime.Date > DateTime.Now.AddDays(-24).Date).ConvertToJson();
// Get Command
string command = getProcessResponse.GetCommand();

Get results by date range

// Gets the processes running on the local computer by date range, in custom collection.
var getProcessResponse = PShell.GetProcess("svchost", "w3wp").WhereObject(c => c.StartTime > DateTime.Now.AddDays(-7) && c.StartTime < DateTime.Now.AddDays(-1)).SelectObject(x => new { x.Name, x.Id, x.StartTime, x.PM, x.WS, x.VM, x.CPU, x.Handles }).ConvertToJson();            
// Get Command
string command = getProcessResponse.GetCommand();

Run command

// Get-Process
ActionResult<GetProcessResponse> resultGetProcess = getProcessResponse.Execute();

if (resultGetProcess.IB_Exception)
    System.Console.WriteLine("Error: {0}",resultGetProcess.Message);            
else 
{
    foreach (var process in resultGetProcess.List)
    {
        System.Console.WriteLine("ID: {0}, Name: {1}, StartTime: {2}, CPU: {3}", process.Id, process.Name, process.StartTime.ToString(PShellSettings.DateFormat), process.CPU);
    }
}

Stop Process (-Force) by date range

// Parameters
DateTime[] dateTimes = new DateTime[] { DateTime.Now.AddDays(-7), DateTime.Now.AddDays(-1) };

// Stop Process (-Force) by date range.
var stopProcessResponse = PShell.GetProcess("w3wp").WhereObject(c => c.StartTime > dateTimes[0] && c.StartTime < dateTimes[1]).StopProcessForce();

// Get Command
string command = stopProcessResponse.GetCommand();

// Execute command
ActionResult<GetProcessResponse> resultStopProcess = stopProcessResponse.Execute();

if (resultStopProcess.IB_Exception)
    System.Console.WriteLine("Error: {0}", resultStopProcess.Message);
else
{
    System.Console.WriteLine("Successful process");
}

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 323 12/30/2021
1.0.3 268 12/21/2021
1.0.2 260 12/20/2021
1.0.1 317 12/20/2021 1.0.1 is deprecated because it is no longer maintained.