locked
How to Install MSI automatically RRS feed

  • Question

  •  Hi,

          After deploying i get msi files. I need to install those files automatically in the server(remote machine)ie;my build is scheduled daily so after the  sucess build the msi also get installed automatically. How can i do this?? now im manually installing those msi's


    Thank You,
    • Edited by Pulikkan Wednesday, November 26, 2008 12:16 PM changed
    Monday, November 24, 2008 2:41 PM

Answers

  • are you installing to the build server? you could use the exec task to call the msi (probably need to pass a silent install switch). if to a remote server, you could copy the file over and use psexec (though you may encounter a few known issues with psexec).
    • Marked as answer by Bill.Wang Monday, December 1, 2008 2:22 AM
    Monday, November 24, 2008 2:51 PM
  • you will have to make sure that the machine you want to run the msi on has the wmi windows installer provider installed.

    i've always had better luck with this if i copy the msi to a share on the machine you are going to install to as well.

    hope this helps

    sample usage:
    <AutomatedInstallation  
                Machine="$(MyServerName)" 
                MsiFile="$(InstallerShare)\$(InstallerName)" 
                CommandLine="TARGETDIR=$(InstallerTargetDirectory) REBOOT=ReallySuppress" 
                TeamFoundationServerUrl="$(TeamFoundationServerUrl)" 
                BuildUri="$(BuildURI)"/> 


    code for msbuild task:
    using System;  
    using System.IO;  
    using System.Management;  
    using Microsoft.Build.Framework;  
     
    namespace Msbuild.Tasks  
    {  
        /// <summary>   
        /// Uses the WMI Windows Installer Provider to install the specified MSI on the specified machine.    
        /// </summary>   
        public class AutomatedInstallation : TeamBuildTask  
        {  
     
            #region "Override implementation of Task"  
     
            protected override bool ExecuteInternal()  
            {  
                Log.LogMessage(MessageImportance.Normal, string.Format("Installing: {0} on {1}", MsiFile, Machine));  
     
                try  
                {  
                    Install();  
                    Log.LogMessage(MessageImportance.Normal, "Installation successful");  
                    return true;  
                }  
                catch (Exception ex)  
                {  
                    Log.LogMessage(MessageImportance.High, ex.ToString());  
                    throw;  
                }  
     
            }  
     
            public void Install()  
            {  
                string path = "//" + Machine + "/root/cimv2:Win32_Product";  
     
                using (ManagementObject installer = new ManagementClass(path))  
                {  
                    installer.Get();  
                    using (ManagementBaseObject inParameters = installer.GetMethodParameters("Install"))  
                    {  
                        inParameters["PackageLocation"] = MsiFile;  
                        inParameters["Options"] = CommandLine;  
                        inParameters["AllUsers"] = "True";  
     
                        using (ManagementBaseObject outParameters = installer.InvokeMethod("Install", inParameters, null))  
                        {  
                            if (!(outParameters["ReturnValue"].ToString() == "0"))  
                            {  
                                throw new ApplicationException("Installation failed. Return code was: " + outParameters["ReturnValue"].ToString());  
                            }  
                        }  
                    }  
                }  
            }  
            #endregion  
     
            #region "Property / Attributes"  
     
     
            public string Machine { get; set; }  
     
            public string MsiFile { get; set; }  
     
            public string CommandLine { get; set; }  
     
            #endregion  
     
     
            protected override string GetBuildStepName()  
            {  
                return "Automated Installation";  
            }  
     
            protected override string GetBuildStepMessage()  
            {  
                return string.Format("Installing {0} on {1} using commandline {2}", MsiFile, Machine, CommandLine);  
            }  
        } 

    you can also download a zip file with this code and other team build samples here
    • Proposed as answer by Paritosh Mhaisekar Wednesday, November 26, 2008 6:39 AM
    • Marked as answer by Bill.Wang Monday, December 1, 2008 2:22 AM
    Tuesday, November 25, 2008 11:01 PM

All replies

  • are you installing to the build server? you could use the exec task to call the msi (probably need to pass a silent install switch). if to a remote server, you could copy the file over and use psexec (though you may encounter a few known issues with psexec).
    • Marked as answer by Bill.Wang Monday, December 1, 2008 2:22 AM
    Monday, November 24, 2008 2:51 PM
  • you will have to make sure that the machine you want to run the msi on has the wmi windows installer provider installed.

    i've always had better luck with this if i copy the msi to a share on the machine you are going to install to as well.

    hope this helps

    sample usage:
    <AutomatedInstallation  
                Machine="$(MyServerName)" 
                MsiFile="$(InstallerShare)\$(InstallerName)" 
                CommandLine="TARGETDIR=$(InstallerTargetDirectory) REBOOT=ReallySuppress" 
                TeamFoundationServerUrl="$(TeamFoundationServerUrl)" 
                BuildUri="$(BuildURI)"/> 


    code for msbuild task:
    using System;  
    using System.IO;  
    using System.Management;  
    using Microsoft.Build.Framework;  
     
    namespace Msbuild.Tasks  
    {  
        /// <summary>   
        /// Uses the WMI Windows Installer Provider to install the specified MSI on the specified machine.    
        /// </summary>   
        public class AutomatedInstallation : TeamBuildTask  
        {  
     
            #region "Override implementation of Task"  
     
            protected override bool ExecuteInternal()  
            {  
                Log.LogMessage(MessageImportance.Normal, string.Format("Installing: {0} on {1}", MsiFile, Machine));  
     
                try  
                {  
                    Install();  
                    Log.LogMessage(MessageImportance.Normal, "Installation successful");  
                    return true;  
                }  
                catch (Exception ex)  
                {  
                    Log.LogMessage(MessageImportance.High, ex.ToString());  
                    throw;  
                }  
     
            }  
     
            public void Install()  
            {  
                string path = "//" + Machine + "/root/cimv2:Win32_Product";  
     
                using (ManagementObject installer = new ManagementClass(path))  
                {  
                    installer.Get();  
                    using (ManagementBaseObject inParameters = installer.GetMethodParameters("Install"))  
                    {  
                        inParameters["PackageLocation"] = MsiFile;  
                        inParameters["Options"] = CommandLine;  
                        inParameters["AllUsers"] = "True";  
     
                        using (ManagementBaseObject outParameters = installer.InvokeMethod("Install", inParameters, null))  
                        {  
                            if (!(outParameters["ReturnValue"].ToString() == "0"))  
                            {  
                                throw new ApplicationException("Installation failed. Return code was: " + outParameters["ReturnValue"].ToString());  
                            }  
                        }  
                    }  
                }  
            }  
            #endregion  
     
            #region "Property / Attributes"  
     
     
            public string Machine { get; set; }  
     
            public string MsiFile { get; set; }  
     
            public string CommandLine { get; set; }  
     
            #endregion  
     
     
            protected override string GetBuildStepName()  
            {  
                return "Automated Installation";  
            }  
     
            protected override string GetBuildStepMessage()  
            {  
                return string.Format("Installing {0} on {1} using commandline {2}", MsiFile, Machine, CommandLine);  
            }  
        } 

    you can also download a zip file with this code and other team build samples here
    • Proposed as answer by Paritosh Mhaisekar Wednesday, November 26, 2008 6:39 AM
    • Marked as answer by Bill.Wang Monday, December 1, 2008 2:22 AM
    Tuesday, November 25, 2008 11:01 PM
  • Hi 

        After deploying the app i got msi and .exe files and i want to install that file in remote server manually ,how can i do that

     

         Regards

         mohsin

    Thursday, June 16, 2011 10:23 AM