tirsdag den 15. januar 2013

How to create a emergincy website for mulible DNS's using Powershell

Okay, There is a lot of ways to ad a IIS site using Powershell so this is defently not the ultimate guide to set it up.
I will in this guide not use the IIS powershell snapin. This is becource this snapin dos not come out of the box when you nstall IIS7 (windows 2008 or 2008R2).
So how to create a script, that create a website.

Well the first thing we need to do is to make a folder to hold our website. This would nomaly be in the Inetpub folder, but it can also be placed in a more decired path.

The powershell command for creating a folder in powershell where $FPath is the folder path.
New-Item -Path $FPath -type Directory

To find out if the parth allready exists use this line, The command return $true or $false.
$FolderPath = Exists-Dir $FPath

Togetter it could look like this
$FolderPath = Exists-Dir $FPath
if ($FolderPath -ne $true){ New-Item -Path $FPath -type Directory}

So now that we have created a folder, we can move on to create a application pool in IIS7. To create this web application we need to import the webapplication modules to modify IIS objects. If you have the IIS snapin installed the server don't import this sanpin. To import the module add this to the first line in the script.
import-module WebAdministration

This will add some new methods to your powershell console. for more information on these please visit:

After we have these methods awalieble we can start manipulating the IIS. To create a weapplication we will use this command where $AppPoolName is the name for the application pool.
New-WebAppPool -Name $AppPoolName

To check if this application pool allready exists, we can use the check status. this will retur a nice ugly error if its not there :o) (Okay I know its a cruel way) but by adding the erroraction silentcontinue we dont have to see it.
Get-WebAppPoolState -Name $AppPoolName
                    
-ErrorAction SilentlyContinue

So to put this together we get this.
$webapp = Get-WebAppPoolState -Name $AppPoolName
                             
-ErrorAction SilentlyContinue
if (-not $webapp)
{
     New-WebAppPool -Name $AppPoolName
}

So now we are ready to make the website. This can be made by using this command.
New-Website -ApplicationPool $AppPoolName
           
-Name $SiteName
            -HostHeader $SiteName
            -PhysicalPath
$FPath
            -Port
80

And to check if the site exists use this command
Get-Website | Where {$_.Name -eq $SiteName};

Bringing this together and creating a site could look like this.
$website = Get-Website | Where {$_.Name -eq $SiteName};
if (-not $website)
{
   New-Website -ApplicationPool $AppPoolName
              
-Name $SiteName
              
-HostHeader $SiteName
              
-PhysicalPath $FPath
               -Port 80
}

So now that we have created the site, its time to add bindings. For multible bindings I have created a xml file, holding all the bindings. Please note that the simple module give no method to check for a binding. This is why this script delete all bindings and adds them again.
Note: You need to ad the primary binding (ex. www.yoursite.com) in the bottom of the xml file.

Here is the compleate function to add bindings.

function AddBindings
{
   $BindingsXMLFile = $dp0 + "\Bindings.xml"
   [xml]$BindingsConfig = [xml](Get-Content $BindingsXMLFile)
   if (-not $BindingsConfig)
   {
      Write-Host "- Bindings configuration file does not exist."
      break 1
   }
   else
  {
      $SiteBindings = Get-WebBinding -Name $SiteName
      if ($SiteBindings)
      {
        foreach ($bind in $SiteBindings)
        {
         Remove-WebBinding -Name $SiteName
                          
-BindingInformation $bind.BindingInformation
        }
   }
     
   $BindingsEnabled = $BindingsConfig.SelectNodes("DeploymentConfig/Bindings/Binding") | where {$_.Enable  -ieq "True"}
   if ($BindingsEnabled)
   {
    foreach ($binding in $BindingsEnabled)
    {
     $BindingHostName = $binding.Hostname
     $BindingPort = $binding.Port
     New-WebBinding -Name $SiteName
                   
-HostHeader $BindingHostName
                    
-Port $BindingPort
                    -ErrorAction
SilentlyContinue
   }
  }
 }
}

Here is an example of the structure in the xml file:
<?xml version="1.0" encoding="utf-8"?>
<DeploymentConfig>
  <Bindings Name="Bindings">
         <Binding Enable="true" Hostname="www.yoursite.com" Port="80"></Binding>
  </Bindings>
</DeploymentConfig>