我有一个MSI文件,我使用WMI远程连接将其复制到客户端计算机。将其复制到某个位置后,我将其安装并更新状态为“安装完成”。但我面临的问题是发送状态已完成的过程是在下面的代码之后立即发生的,即使它失败,状态也将完成。
inParams["CommandLine"] = "msiexec.exe /i " + @"""" + @"c:\windows\temp\" + tempFolderName + @"\Agent.msi" + @"""" + " " + String.Format("/q HEARTBEATSERVERIP={0} CONSOLESERVICEURL={1} HEARTBEATSERVERPORT={2} DOMAINACCOUNT={3} DOMAINACCOUNTPASSWORD={4}", this.heartbeatServerIp, this.consoleServiceUrl, this.heartbeatServerPort, this.userName, this.password);
以上行启动安装过程,下面的代码确定我如何处理整个事情
File.WriteAllText(remoteCopy, cmdFileBuilder.ToString());
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = "cscript";
scriptProc.StartInfo.WorkingDirectory = programDataPath; //<---very important exe location
scriptProc.StartInfo.Arguments = "//B //Nologo remotecopy.vbs";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up
scriptProc.Start();
scriptProc.WaitForExit(); // <-- Optional if you want program running until your script exit
scriptProc.Close();
var agentMsiFileName = "\\" + ipAddress + @"\admin$\Temp\" + tempFolderName + @"\Agent.msi";
//remote copy completed...
//remote copy completed...
ProcessStartInfo info = new ProcessStartInfo();
///Start remote execution
///
try
{
var wmiProcess = new ManagementClass(scope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
ManagementBaseObject inParams = wmiProcess.GetMethodParameters("Create");
inParams["CommandLine"] = "msiexec.exe /i " + @"""" + @"c:\windows\temp\" + tempFolderName + @"\Agent.msi" + @"""" + " " + String.Format("/q HEARTBEATSERVERIP={0} CONSOLESERVICEURL={1} HEARTBEATSERVERPORT={2} DOMAINACCOUNT={3} DOMAINACCOUNTPASSWORD={4}", this.heartbeatServerIp, this.consoleServiceUrl, this.heartbeatServerPort, this.userName, this.password);
ManagementBaseObject outParams = wmiProcess.InvokeMethod("Create", inParams, null);
LogUtilities.Info("Creation of the process returned: " + outParams["returnValue"]);
LogUtilities.Info("Process ID: " + outParams["processId"]);
LogUtilities.Info("Installation completed for : " + discoveredMachineId + " " + ipAddress);
SendAgentStatus(discoveredMachineId, "Completed"); //Installation Completed
我知道我们可以检查我在代码中使用的status codes安装程序,它立即返回'0'而不等待完成,它用于创建进程完成。
ManagementBaseObject outParams = wmiProcess.InvokeMethod("Create", inParams, null);
LogUtilities.Info("Creation of the process returned: " + outParams["returnValue"]);
我也可以用
交叉检查注册表var checkKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\{Product-code}");
但至少需要1-2分钟甚至更长时间,具体取决于系统配置。那么,有没有办法让我们想到完美呢?
我想等到安装完成后再发送状态已完成。