programmatically create environment variables using batch script - c#

On application start, I want to store certain application configuration keys (ex: directory path, database connection string) as an environment variable by running a batch script. I have tried this by writing Powershell script as guided in this article here, which works absolutely fine. I tried the same using the batch file like below but it doesn't seem to be working.
Below is sample of .bat file which I am trying to load/configure at the application start.
EDIT: Correcting .bat the script as suggested by Compo in the comment section below.
#echo off
SET "LOG=api.log"
SET "DATABASE_URL=host=xyz.rs.aws.com dbname=somename user=someuser password=somepassword sslmode=enable"
SET "LISTEN_PROTOCOL=http"
#echo on
Below is sample code used to run the sample.bat file and fetch the value,
string batFilePath = string.Format(#"D:\sample.bat");
Process proc = new Process();
proc.StartInfo.WorkingDirectory = batFilePath ;
proc.StartInfo.CreateNoWindow = true;
proc.Start()
proc.WaitForExit();
// fetch the value set by batch file
var data = Environment.GetEnvironmentVariable("LOG"); // returns null here
However, I also wanted to know, Is it possible to create and store environment variables at the application process level? i.e. I should be able to use these environment variables only within the application level, not at the system/user level.

Related

Scheduler - C# App - Batch - FTP - Works Manually But Not In Schedulers

I have a C# app which executes a batch script, which writes ftp commands to a text file and then executes the text file. It works when executed manually, but not when executed from schedulers such as Windows Task Scheduler (not the one I'll ultimately be using but am using for debugging purposes).
Specifically, when run from the scheduler, the script executes, but the parameters don't get passed, and the ftp script file is not overwritten before it is called, so it only executes whatever script was left from the previous job. It does, however, overwrite the .txt reports that record the ftp session, as intended. Again, when run manually, it all works.
Security policies prevent much tinkering with permissions, but I gave the job account full permissions on all affected folders, and added "log on as batch file" rights to the job account. No parameters need to be passed to the C# application. Any suggestions would be appreciated.
C# Code:
ProcessStartInfo psi = new ProcessStartInfo(scriptFile, paramList);
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
Process p = Process.Start(psi);
//etc...
Batch Script:
Set DIR=%~1
Set SUBDIR=%~2
Set SWITCH=%~3
cd "%DIR%Scripts\
ECHO USER user>FtpScript.txt
ECHO pass>>FtpScript.txt
ECHO lcd "%DIR%FilesToFtp\%SUBDIR%\%SWITCH%">>FtpScript.txt
ECHO lcd "%DIR%FilesToFtp\%SUBDIR%\%SWITCH%"\>>FtpScript.txt
IF "%SWITCH%"=="ONE" ECHO QUOTE SITE LRECL=100 RECFM=FB>>FtpScript.txt
IF "%SWITCH%"=="TWO" ECHO QUOTE SITE LRECL=200 RECFM=FB>>FtpScript.txt
IF "%SWITCH%"=="THREE" ECHO QUOTE SITE LRECL=300 RECFM=FB>>FtpScript.txt
ECHO MPUT "*.*">>FtpScript.txt
ECHO(>>FtpScript.txt
REM other commands, etc...
FTP -d -n -s:"%DIR%Scripts\FtpScript.txt" server > "%FPATH%Reports\%SUBDIR%_%SWITCH%_log.txt"
EXIT 0
Thanks in advance.

Running Bash Commands from C#

I am trying to figure out how to run a bash command from C# running on IIS 7/.Net 4.5.
I've been searching the web and a lot of answers presume you have certain things installed/in place.
I already have Git 1.9.4.msysgit.2 installed with Git Bash and Git Giu. I'm looking for some help as to what else I need installed to run even the simplest of bash commands. And how to run it.
I've looked at posts like bash pipes - I am trying to call script from c# but that uses cygwin. Can I do the same without it and if so, how do I go about it?
Goal
If what I'm asking above doesn't make sense or seems to ask separate questions, here my ultimate goal. I'm trying to write my own server-side git hook. When a developer pushes their commits to our GitHub repo, I want GitHub to call our callback url. I want my callback url to run a git pull command to update our staging server with what was just pushed.
I got to this question based on a previous question I asked at GitHub - setup auto deployment with remote server. based on answers there I'm trying to run a simple command, either but hard coding the command, or putting it in a script and running it, e.g.: cd $REPO_DIR && git pull origin $branch_name.
I am aware of Jenkins and other software, but I want to perform these commands myself vs. installing another software.
If further information is needed please feel free to ask.
Update 1
So based on a few answers below I've come up with the following
using System.Diagnostics;
Process process = new Process();
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.FileName = #"C:\Program Files (x86)\Git\bin\bash.exe";
processStartInfo.WorkingDirectory = #"C:\myrepo\mysite";
processStartInfo.Arguments = "git status";
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.UseShellExecute = false;
process.StartInfo = processStartInfo;
process.Start();
String error = process.StandardError.ReadToEnd();
String output = process.StandardOutput.ReadToEnd();
ViewBag.Error = error;
ViewBag.Ouput = output;
With the code above I am getting "C:/Program Files (x86)/Git/bin/bash.exe": git: No such file or directory. I know the exe is there. What's am I doing wrong?
Update 2
As per #SurgeonofDeath comment I followed this post http://blog.countableset.ch/2012/06/07/adding-git-to-windows-7-path/ and added the paths of Git to my environmental variables. However I still am getting the same issues. Any ideas?
Thanks.
Instead of calling the bash.exe, simply call git and pass the status as argument:
processStartInfo.FileName = "git";
processStartInfo.Arguments = "status";
perhaps i misunderstood your question but what about execve?
here is an excerpt of it's man page.
NAME
execve - execute program
SYNOPSIS
#include <unistd.h>
int execve(const char *filename, char *const argv[],
char *const envp[]);
DESCRIPTION
execve() executes the program pointed to by filename. filename must > be
either a binary executable, or a script starting with a line of > the
form:
#! interpreter [optional-arg]
Check your PATH environment variable and update it
C:/Program Files (x86)/Git/bin/bash.exe": git: No such file or directory
means that it's git which is not found by bash.
1. Check the PATH environment variable in bash (which is and should remain different from Windows one)
Adjust this
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
to make the terminal visible.
In the terminal you will create with the Process.Start()
Type:
echo ${PATH}
2. Update your path
You could update the global path of windows (which requires a restart)
You could update the user path of windows (which should require a logoff, but I'm not sure).
You just set Path to what you like with System.Environment.SetEnvironmentVariable before starting the Process
Additional note:
If you have like me several versions of bash, command interpreters, git and so on, it could be really messy if you try to concatenate all the paths and hope to find the ideal order. You could see some weird behavior of you beloved commands until you realize it's not the one you intend to run ... think of FIND.exe... And I didn't even think of the user-friendly interface of windows to edit environment variables ...

Triggering an application from a windows service cannot find file?

Background:
I created a service that will trigger the execution of an application when certain conditions have been met. This service is setup to run under the same windows user account that is used to log on to the system via RDP. I also created the .NET application that is trigger via this service. This application looks for a configuration file on disk (found in the ProgramData folder for the application) uses the settings found in the configuration file to affect the output of this application.
Problem:
When the application is ran by the user interactively the application runs great. However when the service triggers the application to run it appears that the application is not loading the correct values from the configuration files. It's almost as though the application when ran from a service has its own configuration file, and is not using the one found in ProgramData.
I'm just looking for some insight to why this may be happening. I have seem some odd behavior from Windows 7 and Windows 2008 R2 when running applications via scheduled tasks or as a service. It's almost like interactive applications and service applications have different environments on the same system running as the same user...
Note: The service executable is also found in the same folder as the triggered application. I would expect that the working directory by default would be the services running directory.
public int ExecRun()
{
Process proc = new Process();
proc.StartInfo = new ProcessStartInfo
{
FileName = "C:\\Program Files\\TEST\\runme.exe",
Arguments = "/DS:TEMP"
};
proc.Start();
proc.WaitForExit();
return proc.ExitCode;
}
Try adding the working directory info:
Process proc = new Process();
proc.StartInfo = new ProcessStartInfo
{
FileName = "C:\\Program Files\\TEST\\runme.exe",
WorkingDirectory="C:\\Program Files\\TEST",
Arguments = "/DS:TEMP"
};
It sounds like the service that triggers the execution of the application also needs to set the working directory. If you're using the Process class, you'll need to set the StartInfo.WorkingDirectory property the path where your application resides.
This has been solved.
Unfortunately, I think I wasted all your time with this question. The users is running this service on a second system (other than the one they claimed was having this issue). They copied the same configuration to both systems which would've been fine if they had setup both systems the same way, but alas they did not. The file did not exist on the system throwing the error, but both systems were setup to log exceptions to the same location.
The user had to disable the second service, or setup the configuration file correctly.

Modify remote machine input.properties file from web page

i have build script in remote machine.but i want to start the build from my local machine.so for this i need to update the input.properties file in remote machine and then run the batch file to start the build process. For this i have created one web page
so how can i modify the remote input.properties file and run the batch file in C#.
please give me some suggestion for this.
thanks in advance...
You need to edit the properties file remotely, using a Stream Reader / Stream Writer. There are many ways to do that. You should be able to solve that yourself.
Once you are happy that the properties file is updated correctly you will need to use PSEXEC service to launch the Batch file locally on the User's machine. So in this case, the batch file needs to be copied over to the remote machine.
What I normally do is write the Batch file your intending on using to the remote machine on the fly, and as soon as I can see that the associated process has finished, I remove the batch file from the local machine.
PSEXEC will be ran at your end, you use it to connect up to the end users machine and fire the batch file.
You can create a process like below :-
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\\psexec.exe";
p.StartInfo.Arguments = "\\\\" + computerName + " C:\\YourBatFile.bat";
p.Start();
p.WaitForExit();
This will open a process on the local machine, that will launch your batch file.
I would recommend learning about the PSEXEC Service, its how I've always launched remote processed.
Hope this helps.

execute an EXE on the server

I have an exe which I call from the command line. Is it possible to execute that file on the server? On the computer if the file is located in the folder abc, I go to folder abc and than I execute the batch. Hw do I do this in C#
Code example below, make sure you have your permissions setup correctly:
System.Diagnostics.Process yourProcess = new System.Diagnostics.Process();
// Set the directory
yourProcess.StartInfo.WorkingDirectory = Request.MapPath("~/"); //or wherever your file is
// Set the filename
yourProcess.StartInfo.FileName = Request.MapPath("bla.exe");
// Start the process
yourProcess.Start();
ASP Net - Run Application (EXE) from ASP.Net C#
In server side code certainly, Process.Start(MyExeFile) will do that but, as long as the user account you are running your stuff on can execute it.

Categories

Resources