PSNetUtility 1.0.3
dotnet add package PSNetUtility --version 1.0.3
NuGet\Install-Package PSNetUtility -Version 1.0.3
<PackageReference Include="PSNetUtility" Version="1.0.3" />
<PackageVersion Include="PSNetUtility" Version="1.0.3" />
<PackageReference Include="PSNetUtility" />
paket add PSNetUtility --version 1.0.3
#r "nuget: PSNetUtility, 1.0.3"
#:package PSNetUtility@1.0.3
#addin nuget:?package=PSNetUtility&version=1.0.3
#tool nuget:?package=PSNetUtility&version=1.0.3
PSNetUtility
is a .NET library designed to simplify the integration of PowerShell commands and scripts into C# applications. It provides a variety of methods for executing PowerShell commands, handling parameters, automating tasks, managing services, and more.
Installation
Add PSNetUtility to your project via NuGet Package Manager:
dotnet add package PSNetUtility
Note: This project references the System.Management.Automation package, which is licensed under the MIT License. This package provides essential components for executing PowerShell commands from .NET.
Usage
Below are examples demonstrating how to use each method in the PSNetUtility class.
1. ExecuteCommand
Executes a simple PowerShell command and returns the output.
string result = PSNetUtility.ExecuteCommand("Get-Process");
Console.WriteLine(result);
2. ExecuteCommandWithParameters
Executes a PowerShell command with parameters.
var parameters = new Dictionary<string, object>
{
{ "ServiceName", "wuauserv" }
};
string result = PSNetUtility.ExecuteCommandWithParameters("Start-Service", parameters);
Console.WriteLine(result);
3. ExecuteScript
Loads and executes a PowerShell script from a file.
string result = PSNetUtility.ExecuteScript("path/to/your/script.ps1");
Console.WriteLine(result);
4. ExecuteWithStreams
Executes a PowerShell command and captures both output and error streams.
var (output, error) = PSNetUtility.ExecuteWithStreams("Get-Service");
Console.WriteLine("Output: " + output);
Console.WriteLine("Error: " + error);
5. ExecuteCommandAsync
Executes a PowerShell command asynchronously.
string result = await PSNetUtility.ExecuteCommandAsync("Get-EventLog -LogName System");
Console.WriteLine(result);
6. GetCurrentUser
Gets the current user name using PowerShell.
string currentUser = PSNetUtility.GetCurrentUser();
Console.WriteLine("Current User: " + currentUser);
7. GetOSVersion
Gets the Windows OS version using PowerShell.
string osVersion = PSNetUtility.GetOSVersion();
Console.WriteLine("OS Version: " + osVersion);
8. AutomateTask
Stops processes using too much CPU.
PSNetUtility.AutomateTask();
9. ExecuteAndConvert
Executes a PowerShell command and converts the output to a list of .NET objects.
List<string> services = PSNetUtility.ExecuteAndConvert<string>("Get-Service | Select-Object -ExpandProperty Name");
foreach (var service in services)
{
Console.WriteLine(service);
}
10. ExecuteWithCredentials
Executes a PowerShell command with provided credentials.
string result = PSNetUtility.ExecuteWithCredentials("Get-Process", "username", "password");
Console.WriteLine(result);
11. ExecutePipeline
Executes two PowerShell commands in a pipeline.
string result = PSNetUtility.ExecutePipeline("Get-Process", "Where-Object { $_.CPU -gt 100 }");
Console.WriteLine(result);
12. ExecuteRemoteCommand
Executes a PowerShell command on a remote computer using PowerShell Remoting.
string result = PSNetUtility.ExecuteRemoteCommand("RemoteComputerName", "Get-Process");
Console.WriteLine(result);
13. ExecuteAndRedirectOutput
Executes a PowerShell command and writes the output to a file.
PSNetUtility.ExecuteAndRedirectOutput("Get-Process", "output.txt");
14. ExecuteWithModule
Imports a PowerShell module and executes a command.
string result = PSNetUtility.ExecuteWithModule("PSReadLine", "Get-PSReadLineKeyHandler");
Console.WriteLine(result);
15. SetExecutionPolicy
Sets the PowerShell execution policy for the current process.
PSNetUtility.SetExecutionPolicy("RemoteSigned");
16. GetInstalledPrograms
Gets a list of installed programs using WMI.
string programs = PSNetUtility.GetInstalledPrograms();
Console.WriteLine(programs);
17. CreateScheduledTask
Creates a scheduled task to run a PowerShell script daily at a specified time.
PSNetUtility.CreateScheduledTask("MyTask", "path/to/your/script.ps1");
18. StartService
Starts a Windows service using PowerShell.
string result = PSNetUtility.StartService("wuauserv");
Console.WriteLine(result);
19. StopService
Stops a Windows service using PowerShell.
string result = PSNetUtility.StopService("wuauserv");
Console.WriteLine(result);
20. GetServiceStatus
Gets the status of a Windows service using PowerShell.
string status = PSNetUtility.GetServiceStatus("wuauserv");
Console.WriteLine("Service Status: " + status);
21. AddFirewallRule
Adds a new firewall rule using PowerShell.
string result = PSNetUtility.AddFirewallRule("MyRule", "Inbound", "TCP", 8080);
Console.WriteLine(result);
22. RemoveFirewallRule
Removes an existing firewall rule using PowerShell.
string result = PSNetUtility.RemoveFirewallRule("MyRule");
Console.WriteLine(result);
23. CompressFiles
Compresses files into a ZIP archive using PowerShell.
string result = PSNetUtility.CompressFiles("C:\SourceFolder", "C:\Destination.zip");
Console.WriteLine(result);
24. ExtractFiles
Extracts files from a ZIP archive using PowerShell.
string result = PSNetUtility.ExtractFiles("C:\Source.zip", "C:\DestinationFolder");
Console.WriteLine(result);
25. CreateLocalUser
Creates a new local user account using PowerShell.
string result = PSNetUtility.CreateLocalUser("NewUser", "Password123!");
Console.WriteLine(result);
26. GetSystemUptime
Gets the system uptime in days using PowerShell.
string uptime = PSNetUtility.GetSystemUptime();
Console.WriteLine("System Uptime (days): " + uptime);
27. GetActiveConnections
Gets a list of active network connections using PowerShell.
string connections = PSNetUtility.GetActiveConnections();
Console.WriteLine(connections);
Author
Developed by Håvard Brækken.
Contributing
Feel free to submit issues or create pull requests if you have suggestions or improvements!
License
This project is licensed under the MIT License. See the LICENSE file for more details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- System.Management.Automation (>= 7.4.6)
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.3 | 242 | 11/7/2024 |