Could not make the PowerShell Scriptlet work from C# - 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();

Related

process.start() not working when not in debug mode. Works fine when debugging

I have a pretty simple C# program that needs to silently uninstall an old product. So I set up a process and run it with the right parameters and when I step through the code it works fine every time. When I run it from the command line it fails every time. I'm capturing StandardOut and StandardError and StandardOut just contains the text:
This action is only valid for products that are currently installed.
If I then immediately run it in debug mode it works fine so it's clearly not true that the product isn't installed.
Here's the code:
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = processToRun;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
File.WriteAllText("stdout.txt", output);
string err = process.StandardError.ReadToEnd();
File.WriteAllText("stderr.txt", err);
process.WaitForExit();
And here are the values of:
StartInfo.FileName = "C:\Program Files (x86)\InstallShield Installation Information{325FAC03-900A-4BB2-B45E-1D0EB4D414BE}\setup.exe"
StartInfo.args = /s /f1"C:\Users\User\CommonCustomActions\Uninstall 5.0.0-5.3.2\Uninstall 5.0.0-5.3.2\bin\Debug\Uninstall response files\5.2.1\UNINST-5.2.1.iss"
Any ideas?
p.s. The original installation was built as an InstallShield InstallScript MSI project which is why I need to pass the "response file"

How to use devcon.exe in C# (or other ways to install driver and ping a Hardware ID without infInstall)?

I'm finishing a helper about driver.
Begining, I use DPInst but it can't work in any arguments.
Devcon is available when a batch file call up like devcon install xxxxxxx.inf *xxxxxxx. It needs to edit the devcon.exe's UAC to open. In c# I never worked it.
All the batch command to upgrade permission are failed.
Is there any lib to install the driver without infInstall? Can't work on my driver. I must to point the HardwareID.
Try to call the devcon in cmd:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec");
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.Verb = "runas";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = SourcePath + #"\\x64" + #"\\devcon.exe install xxxx.inf xxxxxxx ";
p.Start();
It also does not work and returns "devcon.exe failed". I guess it's caused by UAC.

c# psexec response get params

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.

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.

Categories