I have written a C# program that uses command line arguments to
turn the firewall on and off.
Process proc = new Process();
string top = "netsh.exe";
proc.StartInfo.Arguments = "**Advfirewall set allprofiles state on**";
proc.StartInfo.FileName = top;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
// MessageBox.Show("Disable");
button1.Text = "Set On";
status = false;
I also run the application with admin permission. The application automatically runs with admin permissions but does not set firewall status on or off.
When I run the same command in cmd(netsh) the firewall is turned on or off.
Does anyone know why this is not working?
Running the same code but removing the ** seems to work. i.e. you need to change this:
proc.StartInfo.Arguments = "**Advfirewall set allprofiles state on**";
to this:
proc.StartInfo.Arguments = "Advfirewall set allprofiles state on";
Note that you should be running the app that starts the process as an administrator and to start the process as admin you can also use:
proc.StartInfo.Verb = "runas";
Related
I have a C# program that launches a child process and captures its output in a string. This works on most Windows machines (Windows 7 and newer), but when Kaspersky anti-virus is present, Process.StandardOutput.ReadToEnd() returns null. There is no error code or exception. The child process is a trusted console application. The process takes 5 or 6 seconds to run.
The code for launching the child process is as follows:
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = "icao.exe";
psi.Arguments = im_path + "image.jpg";
Process p = new Process();
p.StartInfo = psi;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
MessageBox.Show(error);
p.WaitForExit();
int exitCode = p.ExitCode;
MessageBox.Show(exitCode+"");
Why does output end up being null when Kaspersky is present?
My guess is that Kaspersky's heuristics are seeing that your program wants to execute another exe. Because nothing is telling Kaspersky that this is ok, it flags your program as possible malware, because it wants to interface with other programs that are developed by other companies. If you are able to I would try white listing your program with Kaspersky and see if that solves your issue.
I've been searching and experimenting all morning with this one and I'm stumped. I have an aspx page running in IIS and calling the following c# function. I'm trying to have it run a cmd file and return the output from the cmd file. I've experimented with the five different options in the code below:
protected String RunMyCmdFileAndGetResponse() {
Process proc = new Process ();
proc.StartInfo.FileName = #"c:\Windows\System32\cmd.exe";
// proc.StartInfo.Arguments = #"/c echo hello"; <== 1
// proc.StartInfo.Arguments = #"/c c:\mypath\myfile_badname.cmd"; <== 2
// proc.StartInfo.Arguments = #"/c type c:\mypath\myfile.cmd"; <== 3
proc.StartInfo.Arguments = #"/c c:\mypath\myfile.cmd"; // <== 4
// proc.StartInfo.Arguments = #"/c call c:\mypath\myfile.cmd"; <== 5
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
proc.WaitForExit();
return response;
}
Cmd file c:\mypath\myfile.cmd contents are:
#echo test line 1 > c:\mypath\myfilelog.txt
#echo test line 2
This cmd file works as expected when run manually, producing myfilelog.txt and returning test line 2. When executed with the c# code:
Option 1 works - returns 'hello' response as expected.
Option 2 fails as expected, indicating myfile_badname.cmd is not recognized as a valid command
Option 3 works as expected - it returns the contents of myfile.cmd as the response - this confirms I am able to find and read the file.
Option 4 does not work - as near as I can figure, it should. It does not hang up, but also does not return any response at all, and does not execute the cmd file (no myfilelog.txt produced).
Option 5 - same results as option 4.
Note - I've also tried modifying myfile.cmd to remove line 1 (creating the log file) and only leave line 2 to echo a response. Just in case it's a permission issue creating the log file. Same result.
Any help would be appreciated!
Updated to add solution:
The answer from #MaxOvrdrv got me thinking a different way. There does indeed appear to be some kind of limitation when running Process.Start within an IIS context with UseShellExecute = false - if the primary argument is an executable file (cmd file, script file, etc), it will not run it. I tried passing SomeExample.cmd to cmd.exe, and SomeExample.js to cscript.exe.
However... I was able to trick it with a level of indirection, such that the executable file name is no longer the first argument, and it works just fine that way.
To run a cmd file:
string theResponse = RunMyCmdAndGetResponse(#"c:\somepath\mycmd.cmd");
protected String RunMyCmdAndGetResponse(string cmdPath) {
Process proc = new Process ();
proc.StartInfo.FileName = #"c:\Windows\System32\cmd.exe";
proc.StartInfo.Arguments = "/c cmd /c " + cmdPath;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
return response;
}
To run a script file:
string theResponse = RunMyScriptAndGetResponse(#"c:\somepath\myscript.js");
protected String RunMyScriptAndGetResponse(string scriptPath) {
Process proc = new Process ();
proc.StartInfo.FileName = #"c:\Windows\System32\cmd.exe";
proc.StartInfo.Arguments = "/c cscript //nologo " + scriptPath;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
string response = proc.StandardOutput.ReadToEnd();
response += proc.StandardError.ReadToEnd();
return response;
}
Running a batch file or any process per say from an ASPX page is futile as IIS does not run under a true Windows User Context. Because of this, regardless of how many settings and rights you give to the user that the AppPool is running under or whatever type of config changes you make, it simply will never work. I ran into the same problem quite a while back and basically it's impossible.
See my previous questions (and comments), along with accepted answer for possible "conceptual" solution to your current problem, here:
Process.Start won't work
What about not using "cmd"?
Process proc = new Process ();
proc.StartInfo.FileName = #"c:\mypath\myfile.cmd";
I'm setting local auditing policies from a C# .NET program that reads settings from a file then uses Process.Start() with 'cmd' to execute the commands. This way has worked in the past for everything that I've needed it to do (including this exact situation), but recently it's just started to mysteriously fail to set the policies.
Here's the code: (command is of the form "auditpol /set /subcategory:"blah" /success:enable")
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
string result = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
In debug in VS2013 it's applying the policies just fine and even on the same computer in the full on .exe it's applying just fine, but when it gets transferred to another computer it will not set the policies from the auditpol command. Anyone have any ideas what could be happening?
I am trying to restart a windows service from the same windows service with this piece of code
var proc = new Process();
var psi = new ProcessStartInfo();
psi.CreateNoWindow = true;
psi.FileName = "cmd.exe";
psi.Arguments = "/C net stop \"EmailService-3.1.0\ && net start \"EmailService-3.1.0\"";
psi.LoadUserProfile = false;
psi.UseShellExecute = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo = psi;
It is not working and I have no idea why, is there anything I can do to log or determine what is happening or get the output of what is happening when the net stop command is called?
You can redirect the output of the net stop command, but as per TomT's comment, this seems a very roundabout way to restart a service.
psi.Arguments = "/C net stop \"EmailService-3.1.0\" > C:\\svclog.txt "; //&& net start \"EmailService-3.1.0\"";
I can see a missing quotation mark after the name of your service in the stop command. Otherwise it can be a permission issue. Maybe the user with whom your service has logged in does not have enough privileges to stop a service.
I have a bat file that copies files from one location to another.
SET SRC=%1
SET DEST=%2
xcopy /Y/I %SRC%\*.txt %DEST%\temp
echo Done!
I'm trying to run this file via C# program
var psi = new ProcessStartInfo(fileToRun);
psi.Arguments = args;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process cmdProc = Process.Start(psi);
StreamReader output = cmdProc.StandardOutput;
StreamReader errors = cmdProc.StandardError;
cmdProc.WaitForExit();
Bat-file is executed, I can see the 'Done!' message in the output, but files are not copied.
The only way it works is
psi.UseShellExecute = true;
psi.RedirectStandardOutput = false;
psi.RedirectStandardError = false;
But in this case I have to disable output/error redirection and I need them.
So this doesn't work for me.
I have tried to set administrator's username/password
psi.UserName = username;
psi.Password = password;
Logon succeed, but I get the 'The handle is invalid' message in the StandardError Stream.
I guess the process I'm trying to run doesn't have permissions to copy files and
I don't know how to grant him these permissions.
Please, help!
EDITED
Thank you for replies!
I have spend several hours trying to handle this issue and as it always happens I have posted my question and found the solution :)
In order to avoid getting 'The handle is invalid' message you have to
psi.RedirectStandardInput = true;
But now I can see cmd.exe window, if UserName is set, which is bad.
you are missing
psi.Domain = "domain";
psi.Verb ="runas";
//if you are using local user account then you need supply your machine name for domain
try this simple snippet should work for you
void Main()
{
string batchFilePathName =#"drive:\folder\filename.bat";
ProcessStartInfo psi = new ProcessStartInfo(batchFilePathName);
psi.Arguments = "arg1 arg2";//if any
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.Verb ="runas";
psi.UserName = "UserName"; //domain\username
psi.Domain = "domain"; //domain\username
//if you are using local user account then you need supply your machine name for domain
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.Verb ="runas";
Process ps = new Process(psi);
Process.Start(ps);
}