Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of powerShell.AddCommand

I would like to execute this event using c#.

Get-WinEvent -Path 'D:\Events\myevents.evt' -Oldest | Select-Object -Property * | ForEach-Object {$_ | ConvertTo-Json}

I have written upto

 path = "D:\\Events\\myevents.evt";  
 var powerShell = PowerShell.Create();
 powerShell.AddCommand("Get-WinEvent");
 powerShell.AddParameter("Path");
 powerShell.AddArgument(path);
 powerShell.AddParameter("Oldest");
 powerShell.AddCommand("Select-Object");
 powerShell.AddParameter("Property");
 powerShell.AddArgument("*");

I am stuck on writing for ForEach-Object {$_ | ConvertTo-Json}. Let me know how to proceed.

Appreciate help.

like image 979
Sachin Sundar Avatar asked Oct 28 '13 04:10

Sachin Sundar


People also ask

What is PowerShell mainly used for?

As a scripting language, PowerShell is commonly used for automating the management of systems. It is also used to build, test, and deploy solutions, often in CI/CD environments. PowerShell is built on the . NET Common Language Runtime (CLR).

What benefits does PowerShell offer for automating user creation?

With PowerShell you can sequentially execute multiple commands at once or pipe output commands to automate common tasks. Designed for app makers and administrators to automate tasks with environments and associated apps, flows, and connectors.

What cool things can you do with PowerShell?

The uses of PowerShell include adding and deleting accounts, editing groups, and creating listings to view specific types of users or groups. You can also choose to use the Windows PowerShell Integrated Scripting Environment (ISE), a graphic user interface that lets you run commands and create or test scripts.

What is the benefit of learning PowerShell?

It's a unique feature that enables IT professionals to automate and remotely perform special tasks on any Windows PC on the corporate network. Best of all, PowerShell easily provides IT admins with deep visibility and control over all of those network resources just by typing a line of command-line code.


2 Answers

Keith's answer is totally valid if Path come from trusted source. Otherwise, it can be vulnerable for script injection. (demo https://gist.github.com/vors/528faab6411db74869d4)

I suggest a compromised solution: wrap you script in a function, which takes dynamic arguments and Invoke it with AddScript(). Now you have a function in your powershell runspace/session. You can call this function with AddCommand() + AddParameter(). Remember, that you need to call powershell.Commands.Clear() after first Invoke, otherwise commands will be piped.

Code can look like that:

const string script = @"function wrapper($path) {return (Get-WinEvent -Path $path -Oldest | Select-Object -Property * | ForEach-Object {$_ | ConvertTo-Json}) }";
ps.AddScript(script);
ps.Invoke();
ps.Commands.Clear();
ps.AddCommand("wrapper").AddParameter("path", path);
like image 177
xvorsx Avatar answered Sep 28 '22 23:09

xvorsx


You could just use the AddScript method:

powershell.AddScript("Get-WinEvent D:\Events\myevents.evt -Oldest | ConvertTo-Json");

I think you could also simplify that script and pipe directly to ConvertTo-Json.

like image 30
Keith Hill Avatar answered Sep 28 '22 21:09

Keith Hill