{"id":450,"date":"2016-10-20T18:07:47","date_gmt":"2016-10-20T17:07:47","guid":{"rendered":"http:\/\/jonika.nu\/JonasBlogg\/?p=450"},"modified":"2016-10-20T18:23:40","modified_gmt":"2016-10-20T17:23:40","slug":"how-i-automate-website-creation-with-powershell","status":"publish","type":"post","link":"https:\/\/jonika.nu\/JonasBlogg\/archives\/450","title":{"rendered":"How i automate website creation with Powershell"},"content":{"rendered":"<p>If you, like me, create lots and lots of websites almost every day you probably noticed that a lot of the things you do\u00a0are repetitive.<\/p>\n<p>I have a lot of rules that almost all my websites follow.<\/p>\n<ul>\n<li>I name the pool, the site and the folder the same (like site.domain.com)<\/li>\n<li>I always add a binding following the pattern 17000 + site id, so a site with site id 13 has a binding for localhost:17013<\/li>\n<li>I set idle time-out to 0<\/li>\n<li>I add a scheduled recycle at 02:00 (for load balanced environments att set it at 03:00 for the other node)<\/li>\n<li>I always create a scheduled job to take care of log rotation<\/li>\n<\/ul>\n<p>To avoid making misstakes, and to make sure it gets done the same way every time, i use this Powershell. It&#8217;s not perfect, but it works for me, and it does exactly what i need it to.<\/p>\n<p>In the end of the script, where the scheduled job is created, i use a little application called LogRotate which i may publish on the internet. In the mean time, you can use it as a way\u00a0to add a scheduled task.<\/p>\n<p>Maybe it will work for you as well, or maybe you can pick the parts you need. Enjoy!<\/p>\n<pre class=\"lang:ps decode:true\">#\r\n# PowerShell Script used to create new websites. Requires Eleveted PowerShell Console!\r\n# This script: \r\n# \t- Creates a new Applicatiopn Pool with our preferred settings\r\n#\t- Creates a new website using that pool\r\n#\t- Create a scheduled job to manage logfiles\r\n#\r\n# Make sure to edit these parameters before you run the script:\r\n# - $AppPoolName Sets the name of AppPool and WebSite\r\n# - $AppPoolIdentityName and $AppPoolIdentityPwd to set the identity\r\n# - $SiteDirectory is the base path to the folder holding websites\r\n#\r\n\r\n#########################\r\n# Import modules needed #\r\n#########################\r\n\r\nImport-Module WebAdministration\r\n\r\n######################\r\n# Set all parameters #\r\n######################\r\n\r\n#AppPool parameters\r\n\r\n$AppPoolName \t\t\t= \"Test.Domain.Com\"\t            #Name of ApplicationPool\r\n$AppPoolDotNetVersion \t= \"v4.0\"\t\t\t\t\t\t#.NET version for pool\r\n$UseAppPoolIdentity     = 0                             #Set to 1 to user app-pool identity instead of domain account\r\n$AppPoolIdentityName \t= \"domain\\sysAccount\"\t\t\t#Identity to execute pool\r\n$AppPoolIdentityPwd \t= \"P@ssw0rd\"\t\t\t\t\t#Identity password\r\n$AppPoolRecycleTime\t\t= \"02:00\"\t\t\t\t\t\t#Default time is 02:00, on load balanced environments, set first host to 02:00 and second to 03:00\r\n\r\n#WebSite parameters\r\n$SiteName \t\t\t\t= $AppPoolName\t\t\t\t\t#WebSite name, by default the same as name of pool\r\n$SiteDirectory \t\t\t= \"C:\\WebSite\\WebSite\\Sites\\\"\t#Base folder, where the site should be created. Site folder is created automatically\r\n\r\n###########################\r\n# Create application pool #\r\n###########################\r\n\r\n#Create Pool and store it in $AppPool\r\nNew-WebAppPool \u00f1Name $AppPoolName\r\n$AppPool = Get-Item IIS:\\AppPools\\$($AppPoolName) \r\n\r\n#Stop AppPool\r\n$AppPool | Stop-WebAppPool \r\n\r\n#Set additional properties\r\n#Set AppPool Identity and password\r\nif($UseAppPoolIdentity -eq 0)\r\n{\r\n    $AppPool.ProcessModel.identityType = 3\r\n    $AppPool.ProcessModel.Username = $AppPoolIdentityName \r\n    $AppPool.ProcessModel.Password = $AppPoolIdentityPwd\r\n}\r\n\r\n#Set idle time-out to zero\r\n$AppPool.ProcessModel.IdleTimeout = \"0\"\r\n\r\n#Set desired .NET version\r\n$AppPool.ManagedRuntimeVersion = $AppPoolDotNetVersion\r\n\r\n#Set periodic recycle schedule\r\nSet-ItemProperty -Path IIS:\\AppPools\\$($AppPoolName) -Name Recycling.periodicRestart.schedule -Value @{value=$AppPoolRecycleTime}\r\n\r\n#Save and start AppPool\r\n$AppPool | Set-Item\r\n$AppPool | Start-WebAppPool\r\n\r\n#Clear periodic restart time (defaults to 1740 minutes)\r\nSet-ItemProperty -Path IIS:\\AppPools\\$($AppPoolName) -Name Recycling.periodicRestart.time -Value \"00:00:00\"\r\n\r\n\r\n##################\r\n# Create website #\r\n##################\r\n\r\n#Create Website Directory\r\nNew-Item -ItemType directory -Path \"$($SiteDirectory)$($SiteName)\"\r\n\r\n#Create Website and set default binding\r\nNew-Website -Name $SiteName -PhysicalPath \"$($SiteDirectory)$($SiteName)\" -ApplicationPool $AppPoolName -Port \"170$($WebSite.id)\" -HostHeader \"\" -IPAddress \"*\"\r\n\r\n#Get the newly created website and store it in $WebSite\r\n$WebSite = Get-Item IIS:\\Sites\\$($SiteName) \r\n\r\n#Set additional binding based on AppPool name\r\nNew-WebBinding -Name $SiteName -IPAddress \"*\" -Port 80 -HostHeader $AppPoolName.ToLower()\r\n\r\n#Start Website\r\n$WebSite | Start-WebSite\r\n\r\n\r\n######################################\r\n# Create scheduled job for LogRotate #\r\n# This only works on Win2012         #\r\n######################################\r\n#Scheduled task, LogRotate\r\n#Theese values shouldn't be neccesarry to edit. \r\n#Make sure LogRotate exists under Program Files and that you WebSite-folder point to the correct location.\r\n$TaskName \t\t\t\t= \"LogRotate W3SVC$($WebSite.id)\"\r\n$TaskDescription \t\t= \"Archives old logfiles for site $($SiteName)\"\r\n$TaskActionCommand\t\t= \"C:\\Program Files\\LogRotate\\LogRotator.App.exe\"\r\n$TaskArguments\t\t\t= \"C:\\WebSite\\Logs\\W3SVC$($WebSite.id) C:\\WebSite\\Logs\\Backup W3SVC$($WebSite.id) *.log 31 true\"\r\n$TaskPath\t\t\t\t= \"LogRotate\"\r\n\r\n$PsHost = host\r\n$OsVer = [environment]::OSVersion.Version\r\n\r\nIf(($OsVer.Major -ge 6) -and ($OsVer.Minor -ge 2) -and ($PsHost.Version.Major -ge 4))\r\n{\r\n    Import-Module ScheduledTasks \r\n\t$TaskAction = New-ScheduledTaskAction -Execute $TaskActionCommand -Argument $TaskArguments\r\n\t$TaskTrigger = New-ScheduledTaskTrigger -Daily -AT \"05:00\"\r\n\t$Task = New-ScheduledTask -Action $TaskAction -Trigger $TaskTrigger -Description $TaskDescription\r\n\tRegister-ScheduledTask $TaskName -InputObject $Task -TaskPath $TaskPath\r\n}\r\nElse\r\n{\r\n\t\"You need a PowerShell 4 and Windows Server 2012 to create Scheduled Task!\"\r\n\t\"You'll need to configure LogRotate manually.\"\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<a href=\"https:\/\/twitter.com\/jonlin76\" class=\"twitter-follow-button\" data-show-count=\"false\" data-size=\"small\">Follow jonlin76<\/a>\n<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=\/^http:\/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+':\/\/platform.twitter.com\/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');<\/script>","protected":false},"excerpt":{"rendered":"<p>If you, like me, create lots and lots of websites almost every day you probably noticed that a lot of the things you do\u00a0are repetitive. I have a lot of rules that almost all my websites follow. I name the pool, the site and the folder the same (like site.domain.com) I always add a binding &hellip; <a href=\"https:\/\/jonika.nu\/JonasBlogg\/archives\/450\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How i automate website creation with Powershell<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[94,95,116,115],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/450"}],"collection":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":1,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":451,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/450\/revisions\/451"}],"wp:attachment":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}