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()

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: