Deploying IIS websites to a web farm using PowerShell

Home > Blogs > PowerShell > Deploying IIS websites to a web farm using PowerShell

Deploying IIS websites to a web farm using PowerShell

Like This Blog 4 Jason Helmick
Added by September 24, 2012

In a recent article I described the process of implementing a simple Microsoft Network Load Balance using PowerShell for a small web farm. After testing the load balance I need to deploy the new websites. I thought I would share the deployment of one of those with you.

This content applies to Server 2008R2 and Server 2012

The setup

My environment is very simple, I have a management box running the new Windows 8 with the RSAT administration tools. You won’t need the RSAT tools for this process, just PowerShell. You will need to have PowerShell Remoting enabled on the web servers. I also have the website files in a folder structure on my computer. After the website has been created I will deploy those files to all the servers.

Deploying a website to multiple servers

The first step is to open a PowerShell remoting session to the servers. I could have grabbed the server names from Active Directory using the Get-AdComputer cmdlet, but there are only four of them so I’m going to just type the server names and store them to a variable. I’m going to use the variable reference later to simplify the deployment process.

PS> $Servers= ‘web1’, web2’, ‘web3’, ‘web4’ PS>
$session=New-PsSession –ComputerName $servers

Once you have the remote connection to the servers you will need to create a website. I’m going to use the cmdlets from the WebAdministration module on the servers. If you’re working on Server 2008R2 you will need to import this module first, but on Server 2012 the module will dynamically load.

I like to use my own custom application pools with a unique name rather than the default that is created. The first step in this case is to create the new application pool that will hold the website. Also, in my case the web application that I’m deploying needs a different application pool identity. I’m going to change the identity to the Network Service using the Set-ItemProperty cmdlet. The default identity of ApplicationPoolIdentity is the most secured, so only change the identity if you must.

PS> Invoke-Command -Session $Session {New-WebAppPool -Name ProductSite-pool}

PS> Invoke-Command -Session $s {Set-ItemProperty -Path IIS:\AppPools\ProductSite-Pool -Name ProcessModel.IdentityType -value 2}

At this stage I need to create the file structure for the website and deploy the website files.

PS> Invoke-Command -Session $s {New-Item -path c:\sites\ProductSite -ItemType directory -force}

PS> $servers | foreach-Object{copy-item -Path c:\sitefiles\ProductSite\*.* -Destination "\\$_\c$\sites\ProductSite"}

To create the website use the New-WebSite cmdlet and specify the name of the site, the location of the files, the application pool and a unique binding. For my website I’m uniquely identifying it using a host header.

PS> Invoke-Command -Session $Session {New-Website -Name Jason -HostHeader ProductSite.company.com -PhysicalPath C:\sites\jason -ApplicationPool Jason-pool}

After entering an A record for the host header into DNS, test the new site! If you have multiple websites that you want to deploy, or want a rapid disaster recovery option, script the commands into a .PS1

Knowledge is PowerShell,

Jason Helmick

Jason is Director of PowerShell Technologies and an Instructor at Interface Technical Training in Phoenix, AZ.

Upcoming PowerShell classes taught by Jason:

Attend in person or Online with RemoteLive. Also available in Video Training.

Videos You May Like

Windows 10 Managing, Deploying and Configuring – December 2, 2015

0 433 1

In this recorded Windows 10 training webinar from December 2, 2015, Windows Server instructor Rick Trader presents the deployment and management of Windows 10 Enterprise and the new Provisioning capability in Windows 10. Learn how to manage Windows 10 deployments using System Center Configuration Manager, Mobile Device Management and Intune. Also included in his presentation … Continue reading Windows 10 Managing, Deploying and Configuring – December 2, 2015

How to clone a Windows Server 2012 or 2012 R2 Domain Controller

3 1478 3

One of the coolest new features in Window Server 2012 and Windows Server 2012 R2 is the ability to clone a Domain Controller. In the past, if we had virtualized Domain Controllers and we actually took a snapshot of it and then rolled back to that snapshot, it would break the logon service on that … Continue reading How to clone a Windows Server 2012 or 2012 R2 Domain Controller

Detailed Forensic Investigation of Malware Infections – April 21, 2015

4 599 5

How does an investigator hunt down and identify unknown malware? In this recording of our IT Security training webinar on April 21, 2015, Security expert Mike Danseglio (CISSP / CEH) performed several malware investigations on infected computers and identify symptoms, find root cause, and follow the leads to determine what’s happening. He demonstrated his preferred … Continue reading Detailed Forensic Investigation of Malware Infections – April 21, 2015

Write a Comment

See what people are saying...

  1. Pingback: How to deploy PowerShell Web Access PSWA using PowerShell | Interface Technical Training

  2. Pingback: How to deploy PowerShell Web Access PSWA using PowerShell | Interface Technical Training

  3. Avatar Cloi

    Hi Jason,

    The below script doesn’t work if you’re deploying an IIS application on multiple servers and if you’re passing a variable. Please help me fix it.

    # Set Variables
    $Name = “TestApp”
    $filePath = “C:\TestAppFolder\”
    $serverList = “WEB1″,”WEB2″,”WEB3”
    $session=New-PSSession -ComputerName $serverList

    Invoke-Command -session $session {Import-Module WebAdministration}

    Invoke-Command -Session $session {New-WebApplication -Name $Name -Site ‘www.mydomain.com’ -physicalPath $filePath -ApplicationPool NETx86}

    Thanks,
    Cloi

  4. Avatar Mike

    Thanks for the article. How would you do it if you wanted to pass in variables for those values instead of hard-coding them (e.g., $productSitePath instead of -path “c:\sites\ProductSite”)?

Share your thoughts...

Please fill out the comment form below to post a reply.