How to run bat file with required permissions in C# - c#

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

Related

Hide window on process.start with user and password

I have the following code inserted in a console application:
Process delete = new Process();
startInfo.FileName = "schtasks.exe";
startInfo.WorkingDirectory = #"C:\Windows\System32\";
startInfo.UserName = #"AdminUser";
startInfo.Domain = #"mydomain";
startInfo.Password = encryptedPassword;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.Arguments = #" /delete /s 192.168.1.5 /tn Task1 /F";
delete.StartInfo = startInfo;
delete.Start();
delete.WaitForExit();
delete.Close();
This code runs fine, but a new window is launched. I understand that it happens because I run the process with explicit username and password but I really need this and I also need to hide the window, is there a possibility to do that?
As an alternative, I need to execute "schtasks" as an administrator of the remote machine without using the parameters /U and /P of schtasks because I need to encrypt them, so afaik the only possibility is to use Securestring (as I do in my code).
Can you help me?
I think this can be solved by setting the WindowStyle to Hidden:
startInfo.WindoStyle = ProcessWindowStyle.Hidden;

Why is auditpol not applying the local policy settings from C#?

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?

connect compmgmnt.msc to remote host using c#

how can i use compmgmnt.msc to remote host using c#.
i found this...
execute this command in "cmd.exe" it is working and ask you for The password:
/user:administrator \"mmc.exe compmgmt.msc /computer:IZADI-PC\
but i need to know how can i use this command in c#.
i also need to pass the password to this command using c#.
i have username and password of the remote computer and i wanna do everything programatically.
I also visited :
http://www.lansweeper.com/forum/yaf_postst5116_Runas-Custom-Actions.aspx
Process.Start with different credentials with UAC on
Thank you in advanced!!!
anyone write sample code to excecute /user:administrator \"mmc.exe compmgmt.msc /computer:IZADI-PC\ in c#
ProcessStartInfo startInfo = new ProcessStartInfo();
Process myprocess = new Process();
myprocess.StartInfo.CreateNoWindow = true;
myprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myprocess.StartInfo.UseShellExecute = false;
myprocess.StartInfo.Verb = "runas";
myprocess.StartInfo.FileName = "cmd.exe";
myprocess.StartInfo.UserName = "administrator";
myprocess.StartInfo.Password = MakeSecureString("123456");
myprocess.StartInfo.Arguments = "/k mmc.exe compmgmt.msc /computer:PC135-PC";
myprocess.Start();
myprocess.Close();

getting osk.exe to run from C#

I have VS2010, C# program that is setup to build as x86. I have two PCs where they are running. Both are Win 7 Prof, SP1, 32 bits. Both VS2010s are running at Admin level. Within my project I try to execute the line:
Process.Start("c:\\Windows\\System32\\osk.exe"); //win 7 on-screen keyboard
From debug mode-run, on one system it runs fine, on the other, an exception is thrown:
The specified executable is not a valid application for this OS platform.
I have the user control setting in Win 7--User Accounts to "never notify" as suggested from other sites, that did not work.
I have tried: (same result, fail)
Process process = new Process();
process.StartInfo.UseShellExecute = false; //have tried it true also
process.StartInfo.WorkingDirectory = "c:\\";
process.StartInfo.FileName = "c:\\WINDOWS\\system32\\osk.exe";
process.StartInfo.Verb = "runas";
process.Start();
Any ideas what needs to be changed (or do)?
Will this work for you? I altered it a bit using the System.Diagnostics.ProcessStartInfo Class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = #"c:\WINDOWS\system32\";
startInfo.FileName = "osk.exe";
startInfo.Verb = "runas";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
try
{
using(Process process = Process.Start(startInfo))
{
process.WaitForExit();
}
}
catch (Exception)
{
//throw;
}

Executing command-prompt command in C#

:)
I have a software which can be executed via command line, and now I want it to be executed directly from my C# app. Sadly, there is no error but I still can't do it. :(
The path of .exe file of the software is C:\program files\mysoftware.exe
The command I would like to input is
cd c:\program files\mysoftwareFolder
enter
mysoftware.exe d:\myfolder\file1.xxx d:\myfolder\file2.xxx -mycommand
enter
exit
The commands above work so well in the actual command prompt, but they just don't work from my C# code.
Here is the code:
Process cmdprocess = new Process();
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
startinfo.FileName = "cmd";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
System.IO.StreamReader sr = cmdprocess.StandardOutput;
System.IO.StreamWriter sw = cmdprocess.StandardInput;
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine(#"cd" +#"program files\mysoftwarefolder");
sw.WriteLine(#"mysoftware.exe" +#"d:\myfolder\file1.xxx" +#"d:\myfolder\file2.xxx" +#"-mycommand");
sw.WriteLine(#"exit");
sw.Close();
sr.Close();
I guess the incorrect parts might be "startinfo.FileName = "cmd";" or the way I typed the command in the code, but I have no idea how to correct them. :(
Please tell me what I did wrong. I appreciate every answer from you! :)))
UPDATE Thank you for your helps! I tried writing the command in batch file, but it only works in debugging mode. (I forgot to tell you guys that I am developing a web service.) When I run my external project which will use this C# service, it won't work. I don't know whether I should add something to my code or not.
help meeeeee pleaseeeee (T___T)
Write these commands in a batch file and execute the batch file.
In batch file:
cd c:\program files\mysoftwareFolder
mysoftware.exe
d:\myfolder\file1.xxx
d:\myfolder\file2.xxx -mycommand
exit
Code:
Process cmdprocess = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "path to batchfile.bat";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
Instead of:
startinfo.FileName = "cmd";
Directly use
startinfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
Then pass the arguments to the start info as
startinfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
So the whole code looks like:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
p.StartInfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
If you need to see output from your program you can simply use the output string.
2 things: I think you have spacing problems and you're not reading the result of these commands. cmd is probably telling you ..."is not recognized as an internal or external command"
If you look at what you're throwing at cmd, it will be:
echo on
c:
cdprogram files\mysoftware folder
mysoftware.exed:\myfolder\file1.xxx
That won't work when you try it in cmd. CMD is almost certainly kicking back error messages at you, but you're never reading from sr so you'll never know it.
I'd add in some spaces and include all the paths in quotes internally like so:
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine("cd \"program files\\mysoftwarefolder\"");
sw.WriteLine("mysoftware.exe \"d:\\myfolder\\file1.xxx\" d:\\myfolder\\file2.xxx\" -mycommand");
sw.WriteLine(#"exit");

Categories

Resources