Script to create host named site collections
NOTE THIS IS NOT THE BEST RECOMMENDED APPROACH BUT IT WORKS….So if you just want to create HNSC and not understand, then this post is for you. I am writing a better post that explains using the IP address or a SAN cert, this upcoming weekend, May 10, 2020 and will post a link here once it is done.
This post is step 2.
This script works great to create the HNSC after you have created the web application and top site with the script that I created on this other post.
You’ll need to change all the wingtip references to your domain UPN and make sure you have the setup account for your domain entered correctly. If you don’t have a setup account, then you may have erroneously installed SharePoint with the Farm account, in which case you should read my book, Troubleshooting SharePoint to get more on that fiasco. You can replace the server names with as many servers in your farm, in a comma separated list (e.g. “Server1”,”Server2”,”Server3”) and you do not list the SQL server, which should go without saying; but, sometimes it’s better to be safe then sorry.
The farm below had a setup account named wingtip\2016sp_installer and the domain was named wingtip.com with a upn of wingtip.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
################################################################# ## Written by Stacy Simpkins ## ## for ## ## Free Sharing ## ################################################################# $Servers="2016APPSRCH","2016WFEDC" # enter the servername here, or specify a group of servers like this "Server1","Server2" $SharePointSetupAcct = "wingtip\2016sp_installer" #You could replace sp_admin with your setup account name throughout the script #Verify you are logged in as the SharePoint setup account If (!("$env:UserDomain\$env:UserName" -eq $SharePointSetupAcct)){ Throw "Must be logged in as $SharePointSetupAcct please log off and logon with SharePoint setup account domain\SP_Admin" } add-pssnapin *SharePoint* -ErrorAction SilentlyContinue Import-Module WebAdministration #IIS function Test-PsRemoting { param( [Parameter(Mandatory = $true)] $computername ) try { $errorActionPreference = "Stop" $result = Invoke-Command -ComputerName $computername { 1 } } catch { Write-Verbose $_ return $false } ## I’ve never seen this happen, but if you want to be thorough…. if($result -ne 1) { Write-Verbose "`tRemoting to $computerName returned an unexpected result." return $false } return $true } #Verify you are logged in as the SharePoint setup account If (!("$env:UserDomain\$env:UserName" -eq $SharePointSetupAcct)){ Throw "Must be logged in as $SharePointSetupAcct" } #Validate all servers are available before starting Write-Host "Testing connectivity to servers" foreach($s in $Servers){ if (!(Test-PSRemoting $s)) { Write-Host "`tThere was a problem connecting to $s" Write-Host "`tScript Terminated" Exit #Terminates script if a server is unavailable } #end if Write-Host "`tSuccessfully connected to $s" } #end foreach #Instantiate variables and make clean up company name, if needed $CompanyNameWithSpaces = Read-Host -prompt "Input the company name for the URL" $SiteName = Read-Host -prompt "Enter the name for the new Site" Write-Host "`nSite name set to: $SiteName" -ForegroundColor Cyan $primarySiteCollectionOwner = "wingtip\2016sp_installer" $primarySiteCollectionOwnerAcctNoDomain = "2016sp_installer" $CompanyName=$CompanyNameWithSpaces -replace " ",$null #removes spaces $SPWebebAppName = "SharePoint - HNSC" $SPWebebappurl="https://hnsc.wingtip.com" $SPWebebappfqdn="https://hnsc.wingtip.com" $DatabaseName = "PROD_WSS_Content_" + $CompanyName $url=$CompanyName $url=$url -replace " ",$null #removes spaces $url=$url -ireplace "(:\\|:\\)","://" #flip the slashes $url=$url + ".wingtip.com" $url=$url.ToLower() if ($url -match "^https?:\/\/") {} else {$url="https://" + $url;Write-Host "`nURL Changed: $url"} #append https:// if ($url -match "^http:\/\/") { $url=$url -ireplace "http://","https://";Write-Host "`nURL Changed: $url" #replace http with https } #end if $fqdn=$url -ireplace "(http://|https://)","" #Create Database Write-Host "`nCreating Database named: $DatabaseName" New-SPContentDatabase $DatabaseName –WebApplication $SPWebebappurl -WarningSiteCount 0 -MaxSiteCount 1 Write-Host "`nDatabase named: $DatabaseName `n is mounted to Web Application named: $SPWebebAppName" #Create Site Collection # Step 4, create the first HNSC $primarySiteCollectionOwnerAcct = $primarySiteCollectionOwner $PrimarySCOwnerEmail = $primarySiteCollectionOwnerAcctNoDomain + "@wingtip.com" $SecondaryOwnerAcct = "wingtip\stacy" #This name needs to be in your FBA site already, if you dont want to use FBA you could replace with domain\user $SedondaryOwnerEmail = "stacy@wingtip.com" #this can really be any alias for any email $HNSCURL = $url $HNSCDB = $DatabaseName $webApp0URL = $SPWebebappurl Write-Host "`nCreating the new site collection for the Company named: $CompanyNameWithSpaces" -ForegroundColor Green New-SPSite -url $HNSCURL -Name $SiteName -hostheaderwebapplication $WebApp0URL -ownerAlias $PrimarySiteCollectionOwnerAcct -owneremail $PrimarySCOwnerEmail -SecondaryOwnerAlias $SecondaryOwnerAcct -SecondaryEmail $SecondaryOwnerEmail -contentDatabase $HNSCDB -Template STS#0 -WarningAction SilentlyContinue Write-Host "`nsite collection with URL `t$Url for the Company named:`t$CompanyNameWithSpaces has been created please update site collection admins and sharepoint permissions, as may be required" #Adds the binding in IIS function Add-HostHeader($a,$b) #$SPWebebAppName,$fqdn { Import-Module WebAdministration #Adds https binding with host header on a given web site # PowerShell v2 doesn't support SSL Hostheaders. Using appcmd instead. $AllArgs=@("set","site","$a","/+bindings.[protocol='https',bindingInformation='*:443:$b']") &"C:\Windows\System32\inetsrv\appcmd.exe" $AllArgs } #End function #Adds https host header to $SPWebAppName on all servers (SharePoint does not add automatcially) Write-Host "`nAdding HTTPS host header" Invoke-Command -ComputerName $Servers -ScriptBlock ${function:Add-HostHeader} -ArgumentList $SPWebebAppName,$fqdn Write-Host "`tHTTPS host header successfullly added on $servers" Write-Host "`nScript Complete" ################################################################# ## Setting site collection features and settings ## ## Written by Stacy Simpkins ## ## for ## ## Free Sharing ## ################################################################# #CONSTANTS $Servers="2016APPSRCH","2016WFEDC" $SiteCollAdmin = "wingtip\ray" #Need to replace Additional Site collection Admin name with another site collection admin, this is a 3rd admin $SearchPage = "https://searchcenter.wingtip.com/Pages" #INPUT $url=$HNSCURL $url=$url -replace " ",$null #removes spaces $url=$url -ireplace "(:\\|:\\)","://" #flip the slashes $url=$url.ToLower() if ($url -match "^https?:\/\/") {} else {$url="https://" + $url;Write-Host "`nURL Changed: $url"} #append https:// if ($url -match "^http:\/\/") { $url=$url -ireplace "http://","https://";Write-Host "`nURL Changed: $url" #replace http with https } #end if $fqdn=$url -ireplace "(http://|https://)","" $SPSite = Get-SPSite $HNSCurl Write-host "`nStarting to make changes to settings and features for $fqdn" -ForegroundColor Green #Deactivates site collection Features Write-Host "`nDisabling site collection features that conflict with Publishing and FBA" -ForegroundColor DarkYellow Disable-SPFeature "ViewFormPagesLockDown" -url $url -ErrorAction SilentlyContinue -Confirm:$False #Deactivates Limited-access user permission lockdown mode Disable-SPFeature "MDSFeature" -url $url -ErrorAction SilentlyContinue -Confirm:$False #Deactivates Minimal Download Strategy (MDSFeature) Disable-SPFeature -identity 87294c72-f260-42f3-a41b-981a2ffce37a -url $url -ErrorAction SilentlyContinue -Confirm:$False #Deactivates Open Documents in Client Applications by Default (OpenInClient) Disable-SPFeature -identity 8a4b8de2-6fd8-41e9-923c-c7c3c00f8295 -url $url -ErrorAction SilentlyContinue -Confirm:$False #Deactivates Open Documents in Client Applications by Default (OpenInClient) #Activates site collection features Write-Host "`nEnabling site collection features" -ForegroundColor Cyan Enable-SPFeature "BaseSite" -url $url -ErrorAction SilentlyContinue #Activates SharePoint Server Standard Site Collection features (BaseSite) Enable-SPFeature "PremiumSite" -url $url -ErrorAction SilentlyContinue #Activates SharePoint Server Enterprise Site Collection features (PremiumSite) Enable-SPFeature "PublishingSite" -url $url -ErrorAction SilentlyContinue #Activates SharePoint Server Publishing Infrastructure (PublishingSite) Enable-SPFeature "OfficeWebApps" -url $url -ErrorAction SilentlyContinue #Activates Office Web Apps (OfficeWebApps) #Enable-SPFeature "FBAManagement" -url $url -ErrorAction SilentlyContinue #Activates Forms Based Authentication Management feature (FBAManagement) $w = Get-SPWeb $url Get-SPFeature -Web $w | Enable-SPFeature "BaseSite" -url $url -ErrorAction SilentlyContinue #Activates SharePoint Server Standard Site Collection features at the web level (BaseSite) Get-SPFeature -Web $w |Enable-SPFeature "PremiumSite" -url $url -ErrorAction SilentlyContinue #Activates SharePoint Server Enterprise Site Collection features at the web level (PremiumSite) #Activates SharePoint Server Publishing Infrastructure at web level (Publishingweb) $site = Get-SPSite $url $site | Get-SPWeb -limit all | ForEach-Object{ write-host "Activating the PublishingWeb feature on " $_.Url Enable-SPFeature -Identity "PublishingWeb" -Url $_.Url -confirm:$false write-host "Activated the PublishingWeb feature on " $_.Url } #Site Search Settings $WebRoot = $SPSite.RootWeb $WebRoot.AllProperties["SRCH_ENH_FTR_URL"] = $SearchPage $WebRoot.Update() Write-Host "`nSearch Center Page Set" Write-Host "`tSome Awesome Site Collection features successfully enabled" #Add Site Collection administrator $WebRoot.ALLUsers.ADD($SiteCollAdmin, "", "", "") #loginName, email, display name, notes $user = Get-SPUSER -identity $SiteCollAdmin -web $url $user.IsSiteAdmin = $true $user.Update() Write-Host "`nSite Collection Administrators:" $WebRoot.SiteAdministrators | fw Write-Host "`nScript Complete it is OK to close the window now, have a great day!" -ForegroundColor Cyan |
To watch this in action, check out the video on my YouTube channel!