The problem is installation shutdown and uninstall the windows service, but stopped and hang in there in the background. If I run the script outside the code, everything works. just wondering if it is possible to call installation this way in the code and how?
The application is running as Windows Service. This service will call a powershell script to run an installation. In the installation, it will shutdown the Windows service and re-install. the code is below:
string filepath = Path.Combine(requiredFiles.First().Value.Path);
script = $"Start-Process -FilePath {filepath} -ArgumentList \"/S
/Q localhost\"";
_Log.Info($"powershell running: {script}");
var process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = #"C:\windows\system32\windowspowershell\v1.0\powershell.exe";
process.StartInfo.Arguments = script;
process.Start();
process.WaitForExit();
Thank you very much for your help.
I am pretty sure RunspaceFactory will not work.
Related
I am trying to publish a .NET Core 3.1 Solution using the code below. It works fine with the variables given here.
(It is a Console Application that is being debugged and a WinForm that is running this code).
string batchFilePath = #"D:\Program Files (x86)\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat";
string solutionFilePath = #"D:\Documents\Visual Studio\Visual Studio 2022\Projects\Selfmade\Selfmade.sln";
I run the following code and it will debug the solution in the default debug folder.
Process process = new Process();
var command = Environment.GetEnvironmentVariable("ComSpec");
command = #"" + command + #"";
var args = string.Format($"/S/K \" \"{batchFilePath}\" && devenv /build Debug \"{solutionFilePath}\" && exit \"");
process.StartInfo.FileName = command;
process.StartInfo.Arguments = args;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
However this code debugs the solution. I am looking for a simulair way but then to publish this solution. The only issue is that I tried using the dotnet.exe but I cannot seem to figure out any way to publish this solution using a process.
My question is, Is there a way to use a code simulair to this but then for publishing the solution and how do you do it?
I have a simple batch file that uses the VMWare VIX API to run the VMRun.exe executable to start and stop virtual machines in VMWare Workstation 16. The executable is located in my C:\ProgramFiles(x86) directory.
#echo off
cd "C:\PROGRA~2\VMware\VMWARE~2\"
vmrun.exe -T ws Start "C:\Virtual Machines\VMTest\VMTest.vmx"
This batch file runs as expected both when I run it from the Command Prompt and when I call it from a C# .NET console application. The Target Framework for my Console Application is .NET Core 3.1. I execute batch file with the following code:
static void ExecuteCommand(string command)
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = false;
processInfo.UseShellExecute = false;
process = Process.Start(processInfo);
process.WaitForExit();
exitCode = process.ExitCode;
process.Close();
}
However, I am now trying to execute it from a C# .NET Windows Service with a target framework of .NET Framework 4.7.2, and it is not able to run the command. I am using the same method as my console application, and I can run a simple batch file that creates a text file, but I can not get it to work with the VMRun.exe. I've also tried setting the service to run as my user account but that also has not worked. Both the console application and the service are configured to run as x86. Any help would be greatly appreciated.
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
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.
This question already has answers here:
How to run silent installer in C#
(2 answers)
Closed 9 years ago.
I have this C# code:
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "–s –v –qn";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = desktopPath + "\\" + "MyInstaller_7.1.51.14.exe";
Process.Start(psi);
The first line simply grabs the path of my desktop and the rest attempts to run an installer exe file in silent mode. By silent mode I mean, in the background, without the install wizard, or any UI of any sort during installation. The –s –v –qn arguments are there so that that the installation runs in silent mode.
The problem is that when I run the command equivalent of the above in the command prompt, which is this:
C:\Users\ME\Desktop>MyInstaller_7.1.51.14.exe -s -v -qn
The installer runs as wanted, in silent mode.
Unfortunately, the problem is that trying the same thing in C# with the above code does NOT run the installer in silent mode. The installation wizard DOES appear, which is BAD for by purposes.
I'm thinking maybe I need to run this like a service via C# or under the 0 id of the users. Or with an -i switch. I'm not really sure. Can anyone help??
Just for clarification, my question is, how do I write C# code to run my installer.exe file in silent mode, in the background, with no visible UI?
Please help.
This is the correct answer:
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = "/s /v /qn /min";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = newRenamedFile;
psi.UseShellExecute = false;
Process.Start(psi);
The issue was the switches were missing the forward slashes.