C# Process.Start() Argument doesn't work properly - c#

I'm trying to silently install Microsoft Visual C++ 2005 Redistributable using process.start() but have never got any luck. Please anyone help me.
I have the following code
string path = mypath;
startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.Arguments = "/q:a /c:\"msiexec /i vcredist.msi /qn /l*v %temp%\\vcredist_x86.log\"";
startInfo.FileName = path + #"\vcredist_x86.exe";
System.Diagnostics.Process exeProcess = System.Diagnostics.Process.Start(startInfo);
exeProcess.WaitForExit();
the actual commandline argument is
/q:a /c:"msiexec /i vcredist.msi /qn /l*v %temp%\vcredist_x86.log"
I got this command from http://blogs.msdn.com/b/astebner/archive/2006/08/23/715755.aspx
I verified this argument works perfect manually in cmd, but the code never works.
I also tried following code but had no luck as well
startInfo.Arguments = Encoding.Default.GetString(Encoding.UTF8.GetBytes("/q:a /c:\"msiexec /i vcredist.msi /qn /l*v %temp%\\vcredist_x86.log\""));
startInfo.Arguments = "/q:a /c:\"msiexec /i vcredist.msi /qn /l*v %temp%\\\\vcredist_x86.log\""
Please anyone let me know.
Thanks

I don't have the bandwidth to download these things to test, but, try this:
replace the 4 slashes in:
/l*v temp%\\\\vcredist_x86.log\""
with just 2 slashes:
/l*v temp%\\vcredist_x86.log\""

Related

Backup IIS settings with Windows Process

I can backup the settings of IIS with the following commands
//sites
C:\Windows\system32\inetsrv\appcmd.exe list site /config /xml > C:\Temp\iis_config_sites.xml
//apppools
C:\Windows\system32\inetsrv\appcmd.exe list apppool /config /xml > C:\Temp\iis_config_apppool.xml
When I run these commands from the Command Prompt it works fine. The XML files are created. But I wanted to automate this process by executing this command from C# code with System.Diagnostics.Process. For that I use the following code
using (Process process = new Process())
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = #"C:\Windows\system32\inetsrv\appcmd.exe";
startInfo.Arguments = #"list site /config /xml > C:\Temp\iis_config_sites.xml";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
But when I run this code nothing happens. The XML file is not created. But there are no errors and no warnings also. The program that executes this command runs under the Administrator account with Windows Server 2019 and IIS 10. I also tried adding WorkingDirectory, but that did not help either.
I use Process to generate a zipped folders with WinRar and that also works fine.
So if anyone knows what could be the problem that would be great.
The problem is that you are using '>' in the parameters, which is a CMD command to instruct it to write the output of command (appcmd.exe in this case) to specified file path (C:\Temp\iis_config_sites.xml). Which of course doesn't work as the parameter of appcmd.exe.
You have two options:
To use it the following way, which is equivalent of executing the command in CMD:
startInfo.FileName = #"C:\Windows\system32\cmd.exe";
startInfo.Arguments = "/c \"C:\\Windows\\system32\\inetsrv\\appcmd.exe\" list site /config /xml > C:\\Temp\\iis_config_sites.xml";
Note that the /c is a command line option of cmd.exe to execute the rest of parameters as a "command", just as if you entered it into command line window. The rest is just the same command which you typed in the cmd normally.
Use another option provided by appcmd.exe for backing up IIS that avoids the need to redirect output to file. The command is appcmd.exe add backup %backupname%. This command adds a backup of the IIS config and later you can restore it by restore backup $backupname%.

PSEXEC runs successfully via command prompt but fails in ASP .Net/C#

I'm trying to run a command on remote machine using PSEXEC tool, it runs successfully using command prompt but it fails in an asp.net project showing the following output error.
PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark
Russinovich Sysinternals - www.sysinternals.com
The handle is invalid.
Connecting to lnxdevx...
Starting PSEXESVC service on lnxdevx...
Connecting with PsExec service on lnxdevx...
Error deriving session key:.
Here is my sample c# code.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = "-u xxxxxx -p xxxxxxxxxx \\\\lnxdevx -i -d -w \"C:\\DIRECTORY\" cmd.exe /C dir";
p.Start();
string output = p.StandardOutput.ReadToEnd();
string errormessage = p.StandardError.ReadToEnd();
p.WaitForExit();
The problem is that you don't have to open psexec, but cmd and from there you run psexec.
The code you're trying to do is wrong too, you need to use
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"; \\if it is in System32
startInfo.Arguments = "psexec -u xxxxxx -p xxxxxxxxxx \\machine *your stuff*";
process.StartInfo = startInfo;
process.Start();
I'm not testing, but I'm quite sure this will work :)
Although the accepted answer is solving the problem, there is a better solution (at least without involving cmd).
Go to the app pool -> Advanced Settings -> Section "Process Model" -> "Load User Profile".
The default is false. When you change it to true, everything should work.
A bit more of what there is (more of an interpretation, further research is needed).
psexec and all tools from SysInternals require EULA to be accepted. This can be solved by calling them with -accepteula see psexec at ss64
this should be saved in the registry. You have to have a profile for this to be read.
Based on https://nigglingaspirations.blogspot.com/2015/04/psexec-error-deriving-session-key.html

run command line (cmd) that require "Security Message"

I want to run the command line pnputil in c# program. The program needs to install USB driver.
I know how to run cmd in c# program, but I have a different problem:
The driver that I want to install does't have permission of windows.
If I Install the driver via the "device manager->update driver" and choose the path of the driver, I get "Security Message" from windows that "windows can't verify the publisher of this drive software" and let me choose if install the driver or not (of course, if I choose to install - the installing succeeds).
If I run the command from the cmd pnputil -a <path_name_inf> I get this message too and I can install the driver.
But when I try to run the command via c# program - the program runs but the driver is not installed (I also don't get this message).
my code in c#:
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 Pnputil -a <path_name_inf>";
process.StartInfo = startInfo;
process.Start();
How can I do that?
You could try to run the cmd using the runas verb:
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
This parameter causes a privileges elevation. The same thing could be reached when you use "Run as administrator" in explorer.
Your questions is more towards installing unsigned drivers.
Can you please try following steps:
Open a command prompt as an admin and type:
bcdedit -set TESTSIGNING ON
Please refer MSDN link for more infromation on Test Signing. Making Test signing may put a watermark as TEST in desktop waterpaper
I know this is old, but I wanted to share my solution in case it can help someone else.
My specific scenario is that I wanted to add an unsigned driver package to the driver store on Windows 7 Home Premium 64-bit.
Like the OP, this worked as expected (meaning I received the security warning and the driver package was added to the driver store) if I executed pnputil -a <path_to_inf> using a command prompt using "Run as administrator".
However, if I tried to call pnputil from within C#, I was never able to get the security warning and the driver package was not added to the driver store. I had tried using various options (i.e. Verb = "runas", UseShellExecute = true, etc) with the FileName set as "pnputil" or "cmd".
Ultimately, what worked for me was to create a simple batch file that contained the following:
%windir%\sysnative\pnputil /a <path_to_inf>
My C# app would then call this batch file as follows:
Process proc = new Process();
proc.StartInfo.FileName = "<path_to_bat_file>";
proc.StartInfo.Verb = "runas";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
The user would first be prompted because I asked to run with elevated permission followed by the expected security warning. The driver package would then be added to the driver store as expected.
If this does not work as expected, you can add "pause" (without quotes) on a new line after the last command in the batch file and remove the WindowStyle and CreateNoWindow lines from the C# code to see what is happening on the command prompt.

running cmd in asp.net

I want to run cmd in a asp.net application. Here is my code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd";
process.StartInfo.Arguments = "cd C:\\";
process.Start();
The program starts the cmd.exe but instead of "C:>" I see this:
C:\program Files\Common Files\Microsoft Shared\Devserver\10.0>
Could somebody tell me what i do wrong in the code? Thanks in advance!
The Arguments are used as the parameters to the application being called, therefore it's the same as having cmd "cd c:\" in your Run prompt under the Start Menu.
In this instance, what I think you want is the following (instead of the Arguments line)...
process.StartInfo.WorkingDirectory = "c:\\";

How to run cmd.exe and a lot of switches via cmd.exe in Process.Start()

Similar to a previous question, I sometimes run this command on cmd.exe:
Rundll32 Printui.dll,PrintUIEntry /ia /K /q /m "SHARP MX-5500N PS" /h "Windows NT x86" /v 3 /f sn0hwenu.inf
If I want to run this via Process.Start(), is everything up to PrintUIEntry the filename and the rest arguments? Do I need to concat cmd.exe at the front of the above string (I am assuming yes anyway), and if that is the case, then is that the file name and the rest arguments. Is there any rule of thumb here?
Thanks
The filename should be Rundll32.exe. Printui.dll,etc are arguments. You do not need cmd.exe, since rundll32 is its own program.
You can use ProcessStartInfo to specify the file to run (rundll32) and the Arguments property to specify the arguments to use. It is not an array, but a string.
startInfo.Arguments = "/x /y /z";
You don't need cmd.exe because you're starting a process which is going to be rundll32 which doesn't require cmd to host it.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "rundll32.EXE";
startInfo.Arguments = "Printui.dll,PrintUIEntry /ia /K /q /m "SHARP MX-5500N PS" /h "Windows NT x86" /v 3 /f sn0hwenu.inf";
Process.Start(startInfo);
Note that you may need to wrap arguments in quotes (which it looks like you're already doing). For example, if you're giving a path c:\Program Files... you will need to wrap that in quotes or you'll get errors when trying to run the process
startInfo.Arguments = "\"c:\\Program Files\\...\"";
Also keep in mind that specifying rundll32.exe alone might not work. I'm not sure if process.Start uses PATH environment variables to resolve the file so you might need to give the full path to rundll32.exe

Categories