使用WMI从FileShare远程安装应用程序

时间:2013-11-25 22:33:05

标签: c# .net winforms wmi remote-execution

到目前为止,我的代码将在目标计算机上启动一个带有命令行参数的进程(安装应用程序)并等待进程完成,如果我将安装文件复制到该计算机。

我现在的目标是:

  1. 使用命令行参数(安装应用程序)在远程计算机上启动进程。
  2. 不要将文件复制到远程计算机。安装程序文件将位于发件人计算机和远程计算机都可以访问的网络共享上。
  3. 等待该过程完成。
  4. 非常感谢任何帮助!

        private void StartAppAction(string PCName, string Params)
            {   
                //Example of Params \\Server\Folder\Application.EXE /s 
    
                ConnectionOptions conn = new ConnectionOptions();
                conn.Impersonation = ImpersonationLevel.Impersonate;
                conn.Authentication = AuthenticationLevel.Default;
                conn.EnablePrivileges = true;
                ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", PCName), conn);
    
                try
                {
                    manScope.Connect();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    
                ObjectGetOptions objOpt = new ObjectGetOptions();
                ManagementPath manPath = new ManagementPath("Win32_Process");
                ManagementClass manClass = new ManagementClass(manScope, manPath, objOpt);
                ManagementBaseObject inParams = manClass.GetMethodParameters("Create");
                inParams["CommandLine"] = Params;
                ManagementBaseObject outParams = manClass.InvokeMethod("Create", inParams, null);
    
                string query = String.Format("SELECT * FROM __InstanceDeletionEvent WITHIN 3 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.ProcessID = '{0}'", outParams["ProcessId"].ToString());
    
                string scope = @"\\" + PCName + @"\root\CIMV2";
    
                EventWatcherOptions evOp = new EventWatcherOptions(null, new TimeSpan(1, 0, 0), 1);
                ManagementEventWatcher manWatch = new ManagementEventWatcher(scope, query, evOp);
                try
                {
                    ManagementBaseObject watcher = manWatch.WaitForNextEvent();
                    var ID = ((ManagementBaseObject)watcher["TargetInstance"]);                
                    //Process Ended
                }
                catch
                {
                    MessageBox.Show("Unable to watch for the remote process to finish");
                }
            }
    

0 个答案:

没有答案