The Soldier of Fortune

Musing on Microsoft Digital Transformation

Fully configure Host Header for new Web Application with PowerShell

I wrote the post (Fully configure host header for Web application) 2 months ago in order to facilitate how to use host header for your Web application. Using host header allows you to make more easier using SharePoint for end-user. They just need to use friendly URL instead of remembering port number of URL.

In this post, let me share the PowerShell code I made today. PowerShell has a seduction that makes you love when you are in which.

The PowerShell helps you the following:

  • Automatically create new Web Application with Host Header
  • Automatically create new Site collection in the Web Application
  • Automatically remove and then add new binding IIS Web Server
  • Automatically create new Host A record in DNS

Check code out:

Add-PSSnapin Microsoft.SharePoint.PowerShell #Add PowerShell Snap-in for SharePoint

#Set variables for Web Application
$waName = "ICT24H Home"
$waHostHeader = "home.ict24h.intranet"
$waPort = 80
$waAppPool = "SharePointAppPool"
$waAppPoolUserName = "ict24hsp_app"
$databaseName = "HomeDb"
$databaseServer = "sharepoint2010spinstance" #Use named-instance

#Configure Managed Account
$appPoolCred = Get-Credential $waAppPoolUserName
$waAppPoolAccount = New-SPManagedAccount -Credential $appPoolCred

#Create new Web Application
New-SPWebApplication -Name $waName -Port $waPort -HostHeader $waHostHeader -URL ("http://" + $waHostHeader) -ApplicationPool $waAppPool -ApplicationPoolAccount $waAppPoolAccount -DatabaseName $databaseName -DatabaseServer $databaseServer

#Set variables for Site collection
$scName = "Intranet Team Site"
$scURL = ("http://" + $waHostHeader)
#(Optional) Use Get-SPWebTemplate to view the availabe teampltes in SharePoint using SharePoint 2010 Management Shell
$scTemplate = "STS#0"
$scLanguage = 1033 #English
$scOwner = "ict24hthuan"
$w = Get-SPWebApplication $scURL
Write-Host "Creating new site collection in the $waName Web Application...."
New-SPSite -Name $scName -URL $scURL -HostHeaderWebApplication $w -OwenerAlias $scOwner -Language $scLanguage -Template $scTemplate

#Create new Host (A or AAAA) in DNS
$dnsServer = "ad.ict24h.intranet" #This DNS Server stores the Host A record
$dnsZone = "ict24h.intranet"
$hostA = $waHostHeader #use top-level site collection
$class = 1
$ttl = 3600
$ipAddress = "" #Ip Address of SharePoint Web Server
Write-Host "Please wait some minutes for creating new Host A record process...."
$dnsAType = [wmiclass] "\$dnsServerrootMicrosoftDNS:MicrosoftDNS_AType"
$dnsAType.CreateInstanceFromPropertyData($dnsServer, $dnsZone, $hostA, $class, $ttl, $ipAddress)

#Automatically remove and then add new binding Host Header in IIS
Import-Module WebAdministration #Import PowerShell module for IIS
$ipAddress = ""
$protocol = "http"
Write-Host "Please wait some minutes for the binding process...."
Remove-WebBinding -Name $waName -bindingInformation (":" + $waport + ":" + $waHostHeader)
New-WebBinding -Name $waName -Port $waPort -ip $ipAddress -Protocol $protocol -HostHeader $waHostHeader
« »

© 2017 The Soldier of Fortune. Theme by Anders Norén.