install program remotely using c#

  • Thread starter Thread starter okwow123
  • Start date Start date
O

okwow123

Guest
Hello, All.


I am going to install the program remotely using c# application.


But, my code doesn't work.

Anybody to help this code?






using Microsoft.Win32;
using System;
using System.Management;
using System.Runtime.InteropServices;
using System.ServiceProcess;

namespace WMITest
{
class clsMain
{
static void Main(string[] args)
{
RemoteMSI("ipaddress", "msi program path", "property=setting", "username","userpassword", "domain");
Console.ReadKey();
}



public static void RemoteMSI(string machine, string msi, string commandline, string username, string password, string domain)
{
try
{
ConnectionOptions connection = new ConnectionOptions();
connection.Username = username;
connection.Password = password;
ConnectionOptions options = new ConnectionOptions();
string targetIpAddress = machine;
connection.Authentication = System.Management.AuthenticationLevel.Packet;
ManagementScope scope = new ManagementScope(("\\\\" + targetIpAddress), options);
scope.Connect();
ManagementPath p = new ManagementPath("Win32_Product");
ManagementClass classInstance = new ManagementClass(scope, p, null);
ManagementBaseObject inParams = classInstance.GetMethodParameters("Install");
inParams["AllUsers"] = true;
inParams["Options"] = string.Empty;
inParams["PackageLocation"] = "\\\\" + targetIpAddress + "msi program path";//installer is stored at this location
ManagementBaseObject outParams = classInstance.InvokeMethod("Install", inParams, null);
string retVal = outParams["ReturnValue"].ToString();
Console.WriteLine(retVal);

}

catch (ManagementException me)

{

Console.WriteLine(me.Message);

}

catch (COMException ioe)
{
Console.WriteLine(ioe.Message);
}

}

}


}

Continue reading...
 
Back
Top