Saturday, April 9, 2016

Silent Installation of Applications Using C#

How To Silently Install Applications Using C#

There will be situations where you will need to install applications silently without any human intervention. This is a pretty normal use case for System Administrators and they love to automate their tasks. In fact knowledge of this subject may be handy for any technology worker in various circumstances. In this article we will silently install FileZilla on to the computer. Before we proceed further, here are a few things to keep in mind:
  • To install an application silently using the installer exe/package, you must use the Silent Switch for silent installation as described by the Vendor of the Software. For example Silent Switch for FileZilla is /S. It may vary for different applications.
  • Whether the application will install silently depends on how the packaging of the installer exe was done by the Vendor. So keep in mind that not every application can be installed silently.
  • You can use PowerShell cmdlets/ PowerShell Scripts/ Batch Scripts from within your C# application to achieve Silent Installation of applications. In this tutorials we will be using PowerShell cmdlet to achieve the same.
Scenario: In my case here I have the installer exes in "ApplicationRepository" folder in C drive as shown below.



We will only be installing FileZilla as of now but you can extend it for any number of applications as you wish.

Also let us make sure that FileZilla is not installed by looking at installed applications in Control Panel. As we can see below, FileZilla is not currently installed.


Now let us see the code which does the magic i.e. installs application silently.


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;

namespace SilentInstallation
{
    class Program
    {
        static void Main(string[] args)
        {
            ProcessFolder();
        }

        private static void ProcessFolder()
        {
            const string SOURCEFOLDERPATH = @"C:\ApplicationRepository";

            if (Directory.Exists(SOURCEFOLDERPATH))
            {
                Console.WriteLine("Directory exists at: {0}", SOURCEFOLDERPATH);
                if (Directory.GetFiles(SOURCEFOLDERPATH, "*.exe").Length > 0)
                {
                    int count = Directory.GetFiles(SOURCEFOLDERPATH, "*.exe").Length;
                    string[] files = Directory.GetFiles(SOURCEFOLDERPATH, "*.exe");

                    foreach (var file in files)
                    {
                        var fileName = System.IO.Path.GetFileName(file);
                        var fileNameWithPath = SOURCEFOLDERPATH + "\\" + fileName;
                        Console.WriteLine("File Name: {0}", fileName);
                        Console.WriteLine("File name with path : {0}", fileNameWithPath);
                        //Deploy application
                        Console.WriteLine("Wanna install {0} application on this VM? 
                                            Press any key to contiune.", fileName);
                        Console.ReadKey();
                        DeployApplications(fileNameWithPath);
                        Console.ReadLine();
                    }
                }

            }
            else
                Console.WriteLine("Directory does not exist at: {0}", SOURCEFOLDERPATH);

        }


        public static void DeployApplications(string executableFilePath)
        {
            PowerShell powerShell = null;
            Console.WriteLine(" ");
            Console.WriteLine("Deploying application...");
            try
            {
                using (powerShell = PowerShell.Create())
                {
                  //here “executableFilePath” need to use in place of “
                  //'C:\\ApplicationRepository\\FileZilla_3.14.1_win64-setup.exe'”
                  //but I am using the path directly in the script.
                  powerShell.AddScript("$setup=Start-Process 'C:\\ApplicationRepository
                 \\FileZilla_3.14.1_win64-setup.exe' -ArgumentList '/S' -Wait -PassThru");

                    Collection<PSObject> PSOutput = powerShell.Invoke();
                    foreach (PSObject outputItem in PSOutput)
                    {

                        if (outputItem != null)
                        {

                            Console.WriteLine(outputItem.BaseObject.GetType().FullName);
                            Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
                        }
                    }

                    if (powerShell.Streams.Error.Count > 0)
                    {
                        string temp = powerShell.Streams.Error.First().ToString();
                        Console.WriteLine("Error: {0}", temp);

                    }
                    else
                        Console.WriteLine("Installation has completed successfully.");

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error occured: {0}", ex.InnerException);
                //throw;
            }
            finally
            {
                if (powerShell != null)
                    powerShell.Dispose();
            }

        }
    }
}

Now let us see this code in action using the following Screenshots:

Deploying/ Installing Application:


Depending upon the User Control Settings, there may be a prompt to allow our App to install another app(i.e. FileZilla). If you don't want this pop up and want absolute silent installation. Change your settings to Never Notify as shown below and again run the console App if required after changing the setting(Please Note : This setting is not recommended and it's good to revert after the demo).


If everything goes well, the application will be installed successfully as shown below.


Now again go to the Control Panel and refresh the list of installed applications. Now you will be able to see FileZilla installed on your Machine.



In the next version I will try to write a WCF service that can run on a Windows Server and you can hit it with a POST request with relevant information to install an application when required. This concept can be used at many places specially in Server Maintenance and Application Installation.

You can download the above source code and screenshots using the following link:
Download Silent Installation Zip

Hope you enjoyed the article.
Thank You


No comments:

Post a Comment

Thanks for your valuable comment