Showing posts with label Windows Server. Show all posts
Showing posts with label Windows Server. Show all posts

Sunday, October 23, 2016

PowerShell: An Introduction to PowerShell

PowerShell Tutorials (Day 1) : An Introduction to PowerShell


Hey there!! This is the first article of the Series PowerShell Tutorials – A series of articles which will walk you through the basics of PowerShell. It will help you in understanding what’s happening under the hood and will gradually take you to an expert level in PowerShell Scripting if you follow along.

First of all, why Windows PowerShell?

1)     PowerShell provides command-line tools for Windows Administrators
·        Similar to the tools that Unix/Linux administrators have utilized for many years.
·        It also has the following functionalities:
Ø  It is based on Windows Operating System
Ø  Integration with .NET Framework (Just imagine the power you get with PowerShell)

2)     On top of that, PowerShell also provides the following cool features:
·        Cmdlets for performing common system administration tasks to manage the following:
Ø  The Registry
Ø  Services
Ø  Processes
Ø  Event Logs
Ø  Windows Management Instrumentation (WMI)
·        A task-based scripting language and support for existing scripts and command-line tools.
·        Consistent design, common syntax and naming conventions.
·        Support for pipelining.
·        Simplified, command-based navigation of the operating system
§  For example, the registry and other data sources can be accessed by using the same techniques that are used to navigate the file system.
·        Powerful object manipulation capabilities, i.e. objects can be directly manipulated or sent to other tools or databases.
·        Extensible Interface
§  For example, Independent software vendors and enterprise developers can build custom tools and utilities to administer their software.

History of PowerShell
In this section, let’s visit some interesting facts in the History of PowerShell to get a better idea of its usefulness and popularity.

v PowerShell 1.0

Ø  Originally called Monad.
Ø  First introduced in September 2003 at a developer conference.
Ø  Released with a name change to PowerShell in November 2006.
Ø  It was part of:
§   Windows XP SP2/SP3
§  Windows Vista
§  Windows Server 2003

v PowerShell 2.0

Ø  Released in August 2009.
Ø  Included support for:
§  Windows 7
§  Windows Server 2008 R2
Ø  Major improvements and enhancements over 1.0
§  PowerShell remoting greatly simplified, executing commands on remote computer was way easier now.
§  Added hundreds of new Cmdlets and features

v PowerShell 3.0

Ø  Released in 2012.
Ø  More new features and capabilities.
Ø  Introduced support for workflows.
Ø  Improved the graphical PowerShell tool
§  Renamed to Integrated Scripting Environment (ISE).
Ø  Improved the tab completion feature.

v PowerShell 4.0

Ø  Released in 2013, just one year after PowerShell 3.0 was released (by this you can guess the momentum and enthusiasm in the community for PoweShell).
Ø  One really big feature i.e. Desired State Configuration(DSC) was added.
§  Manages the deployment and configuration of Windows Services and their settings.
§  It can manage files, directories and registry settings.
§  Stops and starts any services or processes.
§  Provides cmdlets for user and group account management.
§  Discover current configuration status and restore configuration.
§  Deploy new software.

v PowerShell 5.0

Ø  The PowerShell 5.0 Preview was released in May 2014.
Ø  Introduces PowerShellGet.
§  A new way to discover, install and update PowerShell modules.
Ø  Adds NetworkSwitch cmdlets.
§  Adds the ability to manage Network Switches.
Ø  Introduces OneGet.
§  A new way to discover and install software packages from around the web.

There is much more to PowerShell than we saw here. Stay tuned for the next series of articles on PowerShell Tutorials and I promise you will be able to tame the huge beast called PowerShell. In the next series of articles, we will get our hands dirty and play with PowerShell Scripting.
Cheers!!





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