c# psexec response get params - c#

im trying to execute remote process using psexec in c# application.
this is my code :
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C psexec \\\\"+hostname +" "+command;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
response = process.StandardOutput.ReadToEnd();
process.WaitForExit();
the problem is im not getting response only if command="", i get the definition of the psexec command in response but if command="dir" for exemple i get nothing in response . any help.

You forgot to read standard error as well. Maybe, psexec writes to that.
I suggest you use one of the top voted snippets of stack overflow, to be found by: site:stackoverflow.com process RedirectStandardOutput. There are hundreds of such questions. Most solutions are subtly wrong but certainly better than this one which is clearly incorrect.
This is a good checklist.

Related

Could not make the PowerShell Scriptlet work from C#

I am trying to stop an application pool by issuing the below command from PowerShell
Stop-WebAppPool "Test1234"
This worked. I checked that the "Test1234" Application Pool has stopped.
However, when I made it as a test.ps1 file and invoked the same from my C# code, it didnot worked.
C# Code
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"powershell.exe";
startInfo.Arguments = #"& 'c:\test.ps1'";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
It didn't throw any error but didn't worked. Why this happened and how to get that work?
If you need to invoke powershell from C#, you may consider using System.Management.Automation nuget package. As described in msdn docs, this nuget package will allow you to run powershell as follows:
using System.Management.Automation;
// ...
PowerShell ps = PowerShell.Create();
ps.AddCommand("Stop-WebAppPool");
ps.AddArgument("Test1234");
ps.Invoke();

Argument for ProcessStartInfo needs quotes, always escapes

If I run the following command in a command prompt, it works:
"C:\Program Files (x86)\AppFolder\do.exe"
If I try to run the same thing as a process:
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"/c ""C:\Program Files (x86)\AppFolder\do.exe""";
using (Process process = Process.Start(startInfo))
{
...
}
It does not appear to run. If I look at the startInfo.Arguments, it appears as follows:
/c \"C:\\Program Files (x86)\\AppFolder\\do.exe\"
What am I missing? Can you not pass arguments that have quotes or spaces? I see a lot of examples where people are passing items with spaces/quotes in just fine, but following them just doesn't seem to work. This is the narrowest example I can think of.
EDIT: If I just create a shortcut so I don't use spaces or quotes, it works:
startInfo.Arguments = #"/c D:\_test\Do.lnk";
I do not use '#'. This is correct implementation:
startInfo.Arguments = "/c \"C:\\Program Files (x86)\\AppFolder\\do.exe\"";

Start Process which calls a powershell script error output

I have a c# console app which calls a powershell script as you can see below. If there are any errors in the powershell script, i'd love for it to report back the error to the c# console and then break in the console. How does one go about that? A code example would be greatly appreciated.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"powershell.exe";
startInfo.Arguments = argFinal;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.CreateNoWindow = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Also, the powershell script itself calls some things like 7-zip and such. If they error, i would also like to capture that. Not sure if that'd work differently based on being called from powershell and not the c# console app itself.
You need to set the RedirectStandardError property to true. Then you can read the StandardError stream on the Process object to get any error text. See this MSDN topic for details.

Telnet from command line doesn't work

I want to open Telnet session from command line via .NET.
This command works fine manually:
telnet towel.blinkenlights.nl
So i try to open it via .NET
Process process = new Process();
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.StartInfo.Arguments = "telnet towel.blinkenlights.nl";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
I am using Wireshark to check if this start the traffic and here it seems that nothing happen and i cannot see any Telnet traffic.
If you use ProcessWindowStyle.Normal instead you would see you are not actually executing telnet. You must add the "/C" parameter if you want the CMD window to close after finishing or "/K" if you want it to remain open.
Process process = new Process();
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.StartInfo.Arguments = "/k telnet towel.blinkenlights.nl";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
process.WaitForExit();
After you get the behavior you want, then of course switch back to Hidden.

PGP File Decryption

Need some help with pgp file decryption
anyone have an idea how to do it in c#?
I have it implemented through process.start ("cmd.exe", command)
but its not doing anything other than openning the C:\Windows\System32\IISExpress>
the command line not getting executed,
any help is appreciated.
Code Snippet:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "pgp --decrypt " + inputfile+ " -r \"inputphrase\" --passphrase \"passphrase\" --output " + outputfile+ ".txt";
process.StartInfo = startInfo;
process.Start();
Please suggest if there is way to achieve this.
The better way would be to use PGP (or OpenPGP, which is the same) library for C#/.NET.
There are free and opensource ones (like Bouncycastle), but they lack support, examples, etc.
Also there are better supported commercial libraries (like SecureBlackbox), but they cost some money.

Categories