I'm trying to run a few lines of command in an attempt to automate a work process. I've done some research online to get an idea of what I need to do. However the file remains unaffected. When I run the same command through admin command prompt, the command works.
Commands:
echo 127.0.0.1 webedit.egnyte.com >> "C:\Windows\System32\drivers\etc\hosts"
"%ProgramFiles(x86)%\Egnyte Connect\EgnyteDrive.exe" -command add -l Egnyte -d seagen -sso use-sso -t U -c connect_immediately
I've changed the manifest to "requireAdministrator" and "highestAvailable". However there still is no change to the file.
private void Button1_Click(object sender, EventArgs e)
{
string command1 = "echo 127.0.0.1 webedit.egnyte.com >> \"C:\\Windows\\System32\\drivers\\etc\\hosts\"";
Console.WriteLine("Starting Command 1: " + command1);
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = command1;
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
Console.WriteLine("Completed Command 1 ");
}
The file "host" should have an added line to it.
Related
Hi I am trying to dump a postgres db in a docker. The script I use on powershell is :
docker exec -t timescaledb pg_dumpall -c -U postgres > ..\dump_timescales\dump_prova.sql
It work as expected. I am not able to figure it out how to run it in c#.
I tried the following:
internal static bool dumpTdb(DateTime time)
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "powershell.exe";
startInfo.Arguments = $"/C docker exec -t timescaledb pg_dumpall -c -U postgres > ..\\dump_timescales\\dump_prova_{time}.sql";
process.StartInfo = startInfo;
process.Start();
}
catch (Exception)
{
return false;
}
return true;
}
I get no error however no file is dumped.
What am I missing?
Which is the best practice to dump a db from c#?
Additional info: I am running this function from a Time Triggered Azure function.
process.Start - as the name implies - starts a process. If you exit your program after calling dumpTdb, then all child processes, including your dump process, will be killed.
Call process.WaitForExit() to wait for the dump to finish like this
internal static bool dumpTdb(DateTime time)
{
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "powershell.exe";
startInfo.Arguments = $"/C docker exec -t timescaledb pg_dumpall -c -U postgres > ..\\dump_timescales\\dump_prova_{time}.sql";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
catch (Exception)
{
return false;
}
return true;
}
I'm using WinAppDriver in order to run some test cases on Excel. I'm trying to start the server through the code so that I don't have to manually do it in command line. I have the following code-
public static void StartWinAppServer(int port) {
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = #"C:\Program Files (x86)\Windows Application Driver\";
startInfo.Arguments = $"WinAppDriver {port}";
process.StartInfo = startInfo;
process.Start();
}
Which is called like this-
public static WindowsDriver<WindowsElement> GetWindowsAppDriver (AppName appName) {
string appID = string.Empty;
StartWinAppServer(4723);
switch(appName) {
case AppName.Excel:
appID = #"C:\Program Files\Microsoft Office\root\Office16\Excel.exe";
break;
}
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", appID);
return new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities);
}
This code opens up the CMD but isn't running it. Am I missing something here? I thought the arguments property would've done the trick.
Try adding the /K or /C flag to startInfo.Arguments. This tells cmd.exe to run the following command and then close (in the case of /C) or return to the cmd prompt (in the case of /K)
startInfo.Arguments = $"/C WinAppDriver {port}";
https://ss64.com/nt/cmd.html
I have a few commands I want to run through the CMD (admin):
ipconfig/flushdns
ipconfig /registerdns
ipconfig /release
ipconfig /renew
netsh winsock reset
Anyone can tell me how?
Question of is sort answered here: Running cmd commands with Administrator rights
The code could be duplicated (as shown below):
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 ipconfig /flushdns";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo2 = new
System.Diagnostics.ProcessStartInfo();
startInfo2.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo2.FileName = "cmd.exe";
startInfo2.Arguments = "/C ipconfig /renew";
startInfo2.Verb = "runas";
process2.StartInfo = startInfo2;
process2.Start();
If you are looking to use powershell, you can create a script which will include your commands something like this:
& "command 1";
& "command 2";
etc.
Well... everything is in the title, this is a C# console app and i'm trying to execute these commands... it dosen't seems to work but why ?
It just open a cmd, i don't see anything like a command
static void Main(string[] args)
{
string strCmdText = "mysqlcheck -r JAMFSOFTWARE -u root -p password";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
string strCmdText2 = "mysqlcheck -o JAMFSOFTWARE -u root -p password";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
string strCmdText3 = "mysqlcheck -c JAMFSOFTWARE -u root -p password";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
Edit : The thing is i don't want to put that password in a batch file where it would be easely accessible, so basically any solution would do as long as no one can see what's in the commande file
GOT CLOSER TO A SOLUTION :
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo("mysqlcheck");
startInfo.Arguments = "-c JAMFSOFTWARE -u root -p !!fakepassword!!";
Process process = new Process();
process.StartInfo = startInfo;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
try
{
process.Start();
}
catch
{
}
}
static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
StreamWriter sw = new StreamWriter("C:\\JAMFCHECK.txt");
sw.WriteLine( e.Data);
}
now i don't know why but it ask for a password...
based on your comment, I think you don't need CMD
you can start the 'mysqlcheck' directly using a different executable (see below)
to get all the results from the command-line can be a bit tricky.
I added a working sample at the end of the answer but I'd like to explain it a bit.
set up what you want to execute first
FileInfo executable = new FileInfo(#"C:\Temp\cmd.bat");
ProcessStartInfo startInfo = new ProcessStartInfo(executable.FullName);
startInfo.Arguments = "two arguments";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
make sure to redirect the output (also I don't want to see it)
Now you give your StartInfo to the process
Process process = new Process();
process.StartInfo = startInfo;
because we want to read the Output we need to
process.EnableRaisingEvents = true;
I assigned two events:
process.OutputDataReceived += new ...
process.Exited += new ...
start the process and start reading
process.Start();
process.BeginOutputReadLine();
The batchFile I used gave me the file and the arguments
#echo off
echo %~nx0
echo %1
echo %2
you can call any executable like this (except cmd.exe, it never finishes)
I attached the complete code and also added a Reset-Event to sync the execution. Here is the output from my debug-console:
Beginn
Starting external Application: C:\Temp\cmd.bat
cmd.bat
two
arguments
Finished executing external Application
End
listing:
// using System.Diagnostics;
// using System.Threading;
// using System.IO
private ManualResetEvent mre;
private void button_Click(object sender, EventArgs e)
{
mre = new ManualResetEvent(false);
Debug.WriteLine("Beginn");
FileInfo executable = new FileInfo(#"C:\Temp\cmd.bat");
ProcessStartInfo startInfo = new ProcessStartInfo(executable.FullName);
startInfo.Arguments = "two arguments";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
Process process = new Process();
process.EnableRaisingEvents = true;
process.StartInfo = startInfo;
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.Exited += new EventHandler(process_Exited);
Debug.WriteLine(String.Format("Starting external Application: {0}", executable.FullName));
process.Start();
process.BeginOutputReadLine();
mre.WaitOne();
Debug.WriteLine("End");
}
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Debug.WriteLine(e.Data);
}
void process_Exited(object sender, EventArgs e)
{
Debug.WriteLine("Finished executing external Application");
mre.Set();
}
hope it helps, best regards
You need to start strCmdText with /C:
static void Main(string[] args)
{
string strCmdText = "/C mysqlcheck -r JAMFSOFTWARE -u root -p password";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
string strCmdText2 = "/C mysqlcheck -o JAMFSOFTWARE -u root -p password";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
string strCmdText3 = "/C mysqlcheck -c JAMFSOFTWARE -u root -p password";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
I am trying to pop up a Windows Command prompt and run a ping command when a link button is clicked. Link button looks something like:
<asp:LinkButton runat="server" ID="lbFTPIP" OnCommand="lbFTPIP_OnCommand" CommandArgumnet="1.2.3.4" Text="1.2.3.4"/>
I tried this for OnCommand:
protected void lbFTPIP_OnCommand(object sender, CommandEventArgs e)
{
string sFTPIP = e.CommandArgument.ToString();
string sCmdText = #"ping -a " + sFTPIP;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = sCmdText;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;
p.Start();
}
When I click the link, it opens the command prompt but does not display or execute the command, it just shows the current directory. Not sure what I am missing here.
It is part of a web page, if this makes a difference.
In order to open a console and immediately run a command, you need to use either the /C or /K switch:
// Will run the command and then close the console.
string sCmdText = #"/C ping -a " + sFTPIP;
// Will run the command and keep the console open.
string sCmdText = #"/K ping -a " + sFTPIP;
If you want to institute a "Press any key", you can add a PAUSE:
// Will run the command, wait for the user press a key, and then close the console.
string sCmdText = #"/C ping -a " + sFTPIP + " & PAUSE";
EDIT:
It would probably be best to redirect your output and then display the results separately:
Process p = new Process();
// No need to use the CMD processor - just call ping directly.
p.StartInfo.FileName = "ping.exe";
p.StartInfo.Arguments = "-a " + sFTPIP;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
var output = p.StandardOutput.ReadToEnd();
// Do something the output.
You don't need to execute cmd.exe, just execute ping.exe instead.
string sCmdText = #"-a " + sFTPIP;
Process p = new Process();
p.StartInfo.FileName = "ping.exe";
p.StartInfo.Arguments = sCmdText;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = false;
p.Start();
Also, don't set UseShellExecute = false unless you intend to redirect the output, which I'm surprised you are not doing.
First thing is that you have something strange here CommandArgumnet="1.2.3.4" that is misspelled. The other thing is the /C and a space before ping.