dump dockerized postgres in a local file c# - c#

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;
}

Related

C# Running command prompt as admin

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.

how to handle yes or no question while running a process

I am trying to execute a command by Process in c#, but problem is that that command ask a question (y/n) and process hang there. would you mind recommend me a solution?
public static OutputEventArgs execSync(string exe, string arguments)
{
OutputEventArgs oea = new OutputEventArgs();
try
{
using (Process myProcess = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.FileName = exe;
startInfo.Arguments = arguments;
myProcess.StartInfo = startInfo;
myProcess.Start();
oea.Data = myProcess.StandardOutput.ReadToEnd();
myProcess.WaitForExit();
oea.exitCode = myProcess.ExitCode;
}
}catch(Exception e)
{
oea.Data = e.Message;
oea.ExceptionHappened();
}
return oea;
}
my command output is something like below:
C:\Users\abc>pcli Label -prI:\PVCS\DEVELOPMENT\
-idabcd:abcpass -v'test3' -f -z '/Project1/APPLICATION/ajax_fetchGetCustNew.php' Unknown os = Windows
NT (unknown) Serena PVCS Version Manager (PCLI) v8.4.0.0 (Build 668)
for Windows NT/80x86 Copyright 1985-2010 Serena Software. All rights
reserved. Version "test3" is already defined in archive
"I:\PVCS\DEVELOPMENT\archives\Project1\Application\ajax_fetchGetCustNew.php-arc".
Overwrite? (y/n)
using(var myProcess = new Process()) {
...
myProcess.Start();
myProcess.StandardInput.WriteLine("y"); // Write 'y' to the processes' console input
...
}
Note: This approach is not very reusable.
Using a command line option like /no-confirm (as John Wu suggested in the questions comments) is preferable, if it exists.

C# - Process object not running cmd command

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

How to run some CMD commands one by one (As admin)

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.

A 32Bit porcess can not access modules of a 64bit process

I am trying to execute a cmd process in my c# code. For most of the application it is working smoothly. But a single task i am getting A 32Bit porcess can not access modules of a 64bit process . i am not trying to access a MainModule Property. And the strange thing is When running with IIS Express the above Exception is not being thrown. This is being thrown only when i host my code in IIS.
Below is the code that i using for executing a process
public static int RunProcess(string startInfoArg)
{
int exitcode = 1;
using (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 = startInfoArg;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
exitcode = process.ExitCode;
}
return exitcode;
}

Categories

Resources