I do not have time to develop this any further, but i needed it at work and would like to share it as it has been useful for me.
I hope by sending it here someone will write some tests and be able to check it in if they think appropriate.
It is a task that uses WMI to install an msi file on our qa servers. I needed it to complete my nant script to release our products internally to qa as we like to have qa test from the install that runs for our clients, not something that was xcopied up to the server.
here is its current usage, note the msi was xcopied to the qa server for this to run (it didn't seem to want to run from a network share)
<install machine="${qaserver}"
msiFile="\\${qaserver}\system\application\${project.version}\setup.msi"
commandLine='INSTALLDIR="c:\inetpub\application\${project.version}\"' />
here is the task, it could use some work, and is without tests, sorry :(
I have found that on 2003 server the msi provider necessary is not installed by default, and needs to be added through add remove dialog.
using System;
using NAnt.Core.Attributes;
using NAnt.Core;
using System.Management;
namespace Paisley.Tasks
{
/// <summary>
/// Summary description for RemoteInstallTask.
/// </summary>
[TaskName("install")]
public class InstallTask : Task
{
public InstallTask()
{
}
#region Override implementation of Task
protected override void ExecuteTask() {
Log(Level.Info, LogPrefix + string.Format(" installing: {0} on {1}", MsiFile, Machine));
// connect to root/cimv2 namespace on the target computer using
// moniker and create an object representing a Win32_Product class
string path = @"//" + Machine + "/root/cimv2:Win32_Product";
ManagementObject installer = null;
ManagementBaseObject inParams = null;
ManagementBaseObject outParams = null;
try {
installer = new ManagementClass(path);
installer.Get();
//Get an input parameters object for this method
inParams = installer.GetMethodParameters("Install");
//Fill in input parameter values
inParams["PackageLocation"] = MsiFile;
inParams["Options"] = CommandLine;
inParams["AllUsers"] = "True";
//Execute the method
outParams = installer.InvokeMethod ("Install", inParams, null);
if(outParams["ReturnValue"].ToString() == "0") {
Log(Level.Info, "installation successful");
} else {
Log(Level.Info,"installation failed. Return code was: " + outParams["ReturnValue"].ToString());
}
} catch(Exception ex) {
Log(Level.Error ,ex.ToString());
throw;
} finally {
if(installer != null) installer.Dispose();
if(inParams != null) inParams.Dispose();
if(outParams != null) outParams.Dispose();
}
}
#endregion Override implementation of Task
#region Property / Attributes
[TaskAttribute("machine", Required=true)]
public string Machine {
get {
return machine;
}
set {
machine = value;
}
}
private string machine = null;
[TaskAttribute("msiFile", Required=true)]
public string MsiFile {
get {
return msiFile;
}
set {
msiFile = value;
}
}
private string msiFile = null;
[TaskAttribute("commandLine", Required=false)]
public string CommandLine {
get {
return commandLine;
}
set {
commandLine = value;
}
}
private string commandLine = null;
#endregion
}
}