I have a PowerShell command that looks like this: Get-WmiObject -Query "SELECT CommandLine FROM Win32_Process WHERE Name LIKE ""%Java%"" AND CommandLine LIKE ""%accessToken%"""
My attempt at transforming this to C# looks like this
using System.Management.Automation;
using System.Management.Automation.Runspaces;
String a = String.Format("Get-WmiObject -Query \"SELECT CommandLine FROM Win32_Process WHERE Name LIKE \"\" % Java % \"\" AND CommandLine LIKE \"\" % accessToken % \"\"\"");
Console.WriteLine(getToken(a));
private string getToken(string script)
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(script);
pipeline.Commands.Add("Out-String");
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject pSObject in results)
stringBuilder.AppendLine(pSObject.ToString());
return stringBuilder.ToString();
}
This method only works when there are no parameters so for example if you just want Get-WmiObject
Any help is apreciated!