Run batch file remotely as administrator using psexec in C# - c#

I have a .bat file on the remote machine. when i run manually cmd as admin, executed psexec works correctly. But in C#, i stuck somewhere. I would like to run this .bat file as administrator in C# code. Here is my code. Could someone help me, please?
public void CopyAtoBFolder()
{
int waitForExit = 1000 * 300;
string localDestinationFolderPath = #"D:\Tool\";
string call = localDestinationFolderPath + "CopyAtoBFolder.bat";
Process process = new Process();
process.StartInfo.FileName = this.path + #"\PsExec.exe";
process.StartInfo.Arguments = "-accepteula " + localDestinationFolderPath + #" \\" + ip + " -i -u " + username + " -p " + password + " " + call;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.BeginOutputReadLine();
var ended = process.WaitForExit(waitForExit);
if (!ended)
{
throw new TifPcbaException("Process timed out.");
}
int resultReturnCode = process.ExitCode;
if (resultReturnCode != 0)
{
throw new TifPcbaException("PSExec return code " + resultReturnCode + " shows an error calling " + call + Environment.NewLine);
}
}
And here is the error.
PSExec return code 6 shows an error calling D:\Tool\CopyAtoBFolder.bat

Add a app.manifest in your project and change the following line in app.manifest file:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
This would force your application to run as admin

Related

SQL CLR calling Process.Start hangs or fails

I have checked and there are many posts similar but I cannot solve this. This is a SQL CLR proc to run an exe that parses PDF files. I can run same code as myself in console app. Permissions are not an issue. I am dumping to event log and the ProcessStartInfo is full formed. UseShellExecute - I am not certain. I have also created a .bat file to execute the code - run it manually - it runs fine. Via this CLR program ? Hangs or runs right away but does nothing. At wits end. My goal was to create the CLR using the Nuget from itext but I had a real hard time installing. SQL CLR doesnt support it but I tried anyway.
Thanks in advance.
[Microsoft.SqlServer.Server.SqlProcedure]
public static void PDFToCSVViaExe_CLR()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
string args = #"\\app\Systems\Universal";
args = " \"" + args + "\"" + " \"" + "*" + "\"" + " \"" + "LVM" + "\"";
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = true;
startInfo.FileName = "c:\\SqlCLR\\PDFRipper\\PDFToCSV.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = args;
EventLog.WriteEntry(".NET Runtime", startInfo.FileName.ToString()+" " + args, EventLogEntryType.Warning, 1000);
string folder = "C:\\SqlCLR\\PDFRipper\\LVM.bat";
try
{
//using (new ImpersonationNamespace.Impersonation("domain", "user", "password"))
{
EventLog.WriteEntry(".NET Runtime", System.Security.Principal.WindowsIdentity.GetCurrent().Name + " OR " + Environment.UserName, EventLogEntryType.Warning, 1000);
folder = System.IO.Path.GetDirectoryName(folder);
if (!Directory.Exists(folder))
{
throw (new Exception("Directory does not exist for " + folder));
}
else
{ EventLog.WriteEntry(".NET Runtime", folder + " exists.", EventLogEntryType.Warning, 1000); }
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
}
catch (Exception ex)
{
using (EventLog EventLog = new EventLog("Application"))
{
EventLog.Source = "Application";
//eventLog.WriteEntry(ex.Message);
EventLog.WriteEntry(".NET Runtime", ex.Message + "Trace" + ex.StackTrace, EventLogEntryType.Warning, 1000);
//eventLog.WriteEntry(".NET Runtime", ex.Message, EventLogEntryType.Warning, 1000);
}
}

running a exe file from web api on iis server not working

I want to execute an exe file on to The Web API as and when user request comes. It is working perfectly but when I am hosting web API to server the exe file is not executed when user request comes. What to do for executing the exe file from web API on IIS server?
Here is the process starting code:
public static void Start(long campaign_id, long contact_id, string startDate, string endDate, string user)
{
try
{
//WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = #"C:\";
startInfo.Arguments = "/c sparkclr-submit --master " + ConfigurationManager.AppSettings["SparkMaster"] + " --driver-class-path " + AppDomain.CurrentDomain.BaseDirectory + "Engine\\mysql.jar " + "--exe CmAnalyticsEngine.exe " + AppDomain.CurrentDomain.BaseDirectory + "Engine " + campaign_id + " " + contact_id + " " + startDate + " " + endDate + " " + user;
process.StartInfo = startInfo;
process.Start();
}
catch (Exception e)
{
LogWritter.WriteErrorLog(e);
}
}
How are you starting the exe? You have any error logs?
You can try using the verb runas in Process.Start to execute the exe file as an Administrator.
ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.FileName = myExePath;
proc.CreateNoWindow = false;
proc.UseShellExecute = false;
proc.Verb = "runas"; //this is how you pass this verb

Run the PsExec without knowing the full path in C# Windows application Form

I need help with my application, I created an application that when they hit the update button will execute some PsExec file to remotetly run some functions. the problem comes that works good in my machine locally but my application will be shared to a lot of users. and I don't want to install to all users the PsExec file to their computers, I just want my program to be installed in their computers and just run the program without any issues.
My code works all the application I just need to add that piece of code that will make the program call the PsExec without knowing the whole Path, right now I installed the PsExec file to my Debug folder, which is the folder that is in my project.
So instead of using the whole PsExec path that is on my directory i will like to use the one of my project
I don't want to use this
string leMars21StArguments = #"/C C:\PowerShellRemotly\PsExec.exe " +
#"-i \\VML-2012-QBOOK2 -u mydomain\" + userInfo.Username + " -p " +
userInfo.Password + " -d C:\\batch\\myfile.bat";
I would like something like this where the debug folder of the project, but it doesn't work. is there another to do this?
string leMars21StArguments = #"/C Debug\PsExec.exe " +
#"-i \\VML-2012-QBOOK2 -u mydomain\" + userInfo.Username + " -p " +
userInfo.Password + " -d C:\\batch\\myfile.bat";
Here's the full code:
Process p = new Process();
//assigned from the form txtboxes to the UserInformation class
userInfo.Username = txt_Username.Text;
userInfo.Password = txt_Password.Text;
//string variabes for the cmd arguments
string leMars21StArguments = #"/C C:\PowerShellRemotly\PsExec.exe -i " +
#"\\VML-2012-QBOOK2 -u mydomain\" + userInfo.Username + " -p " +
userInfo.Password + " -d C:\\batch\\myfile.bat";
if (userInfo.Username == string.Empty)
{
MessageBox.Show("Username cannot be null, please try again");
}
if (userInfo.Password == string.Empty)
{
MessageBox.Show("Password cannot be null, please try again");
}
if (cmb_DatabaseSelection.SelectedItem == "location")
{
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = leMars21StArguments;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string errOutput = p.StandardError.ReadToEnd();
p.Close();
MessageBox.Show(errOutput);
}
If you add it to your program, then you can just reference the location relative to your .exe where you place it.
For example, if you add a new folder called "Tools" to your project, and then add psexec.exe there, and set it to "Copy if newer" (or "Copy always"):
Then you can always get the path to it by grabbing the directory that your .exe is in and navigating to the "Tools" folder (note that you will need a reference to System.Reflection to use Assembly.GetExecutingAssembly):
var thisExeDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var psExecPath = Path.Combine(thisExeDirectory, "Tools", "psexec.exe");
As an example:
var userInfo = new UserInfo {Password = "LetMeIn", Username = "rufusl"};
var thisExeDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var psExePath = Path.Combine(thisExeDirectory, "Tools", "psexec.exe");
var leMars21StArguments =
"/C \"" + psExePath + #""" -i \\VML-2012-QBOOK2 -u mydomain\" + userInfo.Username +
" -p " + userInfo.Password + #" -d C:\batch\myfile.bat";
Console.WriteLine(leMars21StArguments);
Output

Error calling unzip with system.process.start()

Having a weird problem. I have this C# snippet of code I am using to a extract self-extracting .exe on a CentOS 6.5 machine with Mono:
try
{
//new bash process
Process bash = new Process();
bash.StartInfo.FileName = "/bin/bash";
string command = #"unzip " + Globals.fiberDirectory + #"""" + file + ".exe" + #"""";
//Check command string
Console.WriteLine(command);
bash.StartInfo.Arguments = "-c \" " + command + " \"";
bash.StartInfo.UseShellExecute = false;
bash.Start();
}
catch (Exception e)
{
Console.WriteLine(" ...Not Loaded! - " + e.Message);
}
finally
{
}
It outputs this:
unzip "/home/pan/maps/data/telus/FVData/Fiber/5656 all.exe"
unzip: cannot find or open /home/pan/maps/data/telus/FVData/Fiber/5656, /home/pan/maps/data/telus/FVData/Fiber/5656.zip or /home/pan/maps/data/telus/FVData/Fiber/5656.ZIP.
when I run this command from my shell manually:
unzip /home/pan/maps/data/telus/FVData/Fiber/"5656 all.exe"
It works perfectly. Any ideas?

Executing Process not working

The situation:
I want to modify the folder quotas on my FileServer through a process executing dirquota.exe
The Problem:
The Process being executed gives no result at all
So Far:
I've redirected the process and arguments being executed on my FileServer to take a closer look what's happening exactly on the serverside.
The executed process gave no exception and everything went just fine, it seemed..
When looking at the current folder quota's on my FileServer nothing has changed..I decided to copy paste my arguments in a CMD.exe on the server, then it all went fine...
I cannot figure why it is not working on my FileServer, probably somthing simple but I need some help here
Important Info:
I'm installing a Windows Service on my FileServer and calling the Method through SOUPUI (This is all working fine).
The installed service is running as a Domain admin and has all the required rights to perform these actions
The Class
public class Quota
{
public string FolderLocation;
public int SizeInMB;
public string FileServerName;
}
The Method
public string SetFolderQuota(Quota quota)
{
Process QuotaProcess = new Process();
QuotaProcess.StartInfo.RedirectStandardOutput = false;
QuotaProcess.StartInfo.FileName = #"cmd.exe";
QuotaProcess.StartInfo.UseShellExecute = true;
QuotaProcess.StartInfo.Arguments = "/C " + "dirquota Quota Add /PATH:" + '"' + quota.FolderLocation + '"' + " /Limit:" + quota.SizeInMB + "mb" + " /remote:" + quota.FileServerName;
try
{
QuotaProcess.Start();
}
catch(Exception Ex)
{
return Ex.Message;
}
return "Correctly Executed: " + QuotaProcess.StartInfo.FileName + QuotaProcess.StartInfo.Arguments;
}
Found The Problem
dirquota.exe is redirected using Windows-on Windows 64-bit redirection. What's happening is that my launch request (from a 32-bit process) is being redirected to %windir%\SysWOW64\dirquota.exe. Since there's no 32-bit version of this particular executable on 64-bit installs, the launch fails. To bypass this process and allow my 32-bit process to access the native (64-bit) path, I have to reference %windir%\sysnative instead
The Code
public string SetFolderQuota(Quota quota)
{
string FileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows),#"sysnative\dirquota.exe");
Process QuotaProcess = new Process();
QuotaProcess.StartInfo.RedirectStandardOutput = false;
QuotaProcess.StartInfo.FileName = FileLocation;
QuotaProcess.StartInfo.UseShellExecute = true;
QuotaProcess.StartInfo.Arguments = " Quota Add /PATH:" + '"' + quota.FolderLocation + '"' + " /Limit:" + quota.SizeInMB + "mb" + " /remote:" + quota.FileServerName;
try
{
QuotaProcess.Start();
}
catch(Exception Ex)
{
return Ex.Message + Environment.NewLine + "FileLocation: " + FileLocation;
}
return "Correctly Executed: " + QuotaProcess.StartInfo.FileName + QuotaProcess.StartInfo.Arguments;
}
Best if you can redirect the output of Process to a log file and see what is the actual exception..
ProcessStartInfo process = new ProcessStartInfo
{
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = #"cmd.exe",
Arguments = "/C " + "dirquota Quota Add /PATH:" + '"' + quota.FolderLocation + '"' + " /Limit:" + quota.SizeInMB + "mb" + " /remote:" + quota.FileServerName
};
Process p = Process.Start(process);
string output = p.StandardOutput.ReadToEnd();
Log the value of output to get the exact exception caused by execution of this command

Categories

Resources