11

Using PowerShell and Web Platform Installer to install Azure PowerShell cmdlets

Web Platform Installer or Web PI is a free tool that makes getting the latest components of the Microsoft Web Platform, including Internet Information Services (IIS), SQL Server Express, .NET Framework and Visual Web Developer easy. The Web PI also makes it easy to install and run the most popular free web applications for blogging, content management and more with the built-in Windows Web Application Gallery. A lot of stuff that you want to do on Azure requires Web PI. For example, the Azure PowerShell cmdlets can be installed using Web PI.

In this article, I am going to show you how to use Web PI .NET interfaces in PowerShell.

First of all, we need the Web Platform Installer. You can download and install it from http://www.microsoft.com/web/downloads/platform.aspx. Once you have Web PI installed, you can access its API by loading the Microsoft.Web.PlatformInstaller namespace.

[reflection.assembly]::LoadWithPartialName("Microsoft.Web.PlatformInstaller") | Out-Null

The ProductManager class provides access to the product feeds. We can use this class to list the products available for install using Web PI. We can create an instance of this class using the New-Object cmdlet.

$ProductManager = New-Object Microsoft.Web.PlatformInstaller.ProductManager
$ProductManager.Load()

We need to load the default feed by calling the Load() method. It is not necessary to specify a URI as an argument here. It takes the value of DefaultFeed property and loads it. Once we load the default feed, we can see a list of products by accessing the Products property.

$ProductManager.Products | Select Title, Version, Author | Out-GridView

webpi

We can filter the list of product by using the ProductId property.

$ProductManager.Products | Where-Object { $_.ProductId -like "*PowerShell*" } | Select Title, Version | Out-GridView

Let us see an example of using Web PI to install a product. For the purpose of this demonstration, I will select “Windows Azure PowerShell”.

$product = $ProductManager.Products | Where { $_.ProductId -eq "WindowsAzurePowerShell" }

webpi2

We need an instance of the InstallerManager class to perform the package install.

$InstallManager = New-Object Microsoft.Web.PlatformInstaller.InstallManager
$Language = $ProductManager.GetLanguage("en")
$installertouse = $product.GetInstaller($Language)

I have also set the language to English so that I get the right installer for my platform. We now need to get the installer for downloading and installing the package. Note that to be able to install packages, you need administrative privileges. So, if you are installing packages, you need to elevate the console.

$installer = New-Object 'System.Collections.Generic.List[Microsoft.Web.PlatformInstaller.Installer]'
$installer.Add($installertouse)
$InstallManager.Load($installer)

We have now setup the install manager for the right package. We can download the installer package by using the DownloadInstallerFile() method.

$failureReason=$null
foreach ($installerContext in $InstallManager.InstallerContexts) {
    $InstallManager.DownloadInstallerFile($installerContext, [ref]$failureReason)
}

Finally, we start the package installation by the StartInstallation() method.

$InstallManager.StartInstallation()

Once the installation is complete, the StartInstallation() method returns $true if everything is successful. You can review the logs at $Product.Installers[0].LogFiles. Here is the complete script that can install Azure PowerShell cmdlets using PowerShell and Web PI.

[reflection.assembly]::LoadWithPartialName("Microsoft.Web.PlatformInstaller") | Out-Null

$ProductManager = New-Object Microsoft.Web.PlatformInstaller.ProductManager
$ProductManager.Load()
$product = $ProductManager.Products | Where { $_.ProductId -eq "WindowsAzurePowerShell" }

$InstallManager = New-Object Microsoft.Web.PlatformInstaller.InstallManager

$Language = $ProductManager.GetLanguage("en")
$installertouse = $product.GetInstaller($Language)

$installer = New-Object 'System.Collections.Generic.List[Microsoft.Web.PlatformInstaller.Installer]'
$installer.Add($installertouse)
$InstallManager.Load($installer)

$failureReason=$null
foreach ($installerContext in $InstallManager.InstallerContexts) {
    $InstallManager.DownloadInstallerFile($installerContext, [ref]$failureReason)
}
$InstallManager.StartInstallation()
Filed in: Articles, Azure, Online Only Tags: , ,

11 Responses to "Using PowerShell and Web Platform Installer to install Azure PowerShell cmdlets"

  1. Eric Brown says:

    This is AWESOME! How can I generate a list of just the installed applications in WPI 5.0?
    Also, is there a way to copy the currently installed list (Similar to how we do it with Roles and services) and apply it to a new server VM install.

    I am trying to streamline node installs into an CDN/ECN environment as traffic increases.

    Thanks in advance

    Eric Brown

  2. Ravikanth C says:

    Hey Eric, there is a PowerShell module written by one of the PowerShell MVPs.
    https://github.com/guitarrapc/WebPlatformInstaller

    You can use this module to achieve what you want.

  3. Martin says:

    This fails with error:
    Exception calling “DownloadInstallerFile” with “2” argument(s): “The InstallerContext passed to this method requires a non-Null
    InstallerFile.”

    $InstallManager.DownloadInstallerFile($installerContext, [ref]$failureReason …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

  4. Matt says:

    Same as Martin…errors.

  5. David Tesar says:

    Thanks for this sample! The script didn’t work as-is, but when I changed this line:
    $product = $ProductManager.Products | Where { $_.ProductId -eq “WindowsAzurePowerShell” }

    to

    $product = $ProductManager.Products | Where { $_.ProductId.Equals(‘WindowsAzurePowershell’) }

    Everything worked!

  6. Akshay says:

    Hi,

    Is there a way to wait for the installation to complete?

    => $InstallManager.StartInstallation()

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: