{"id":410,"date":"2016-01-08T14:24:20","date_gmt":"2016-01-08T13:24:20","guid":{"rendered":"http:\/\/jonika.nu\/JonasBlogg\/?p=410"},"modified":"2016-10-20T09:26:51","modified_gmt":"2016-10-20T08:26:51","slug":"how-to-create-websites-and-more-with-powershell","status":"publish","type":"post","link":"https:\/\/jonika.nu\/JonasBlogg\/archives\/410","title":{"rendered":"How to create Websites (and more) with PowerShell?"},"content":{"rendered":"<p>If you, like me, are maintaining lots of websites, you probably want to script as much as you can. It&#8217;s time consuming to change a setting for 20+ websites, and it&#8217;s also a great chance you miss something in the process.<\/p>\n<p>PowerShell is really neat in these cases, and it gets better for every new release as well.<\/p>\n<p>This is a script i use to do the following:<\/p>\n<ol>\n<li>Create a new application pool, with the preferred .NET version, identity and a few other settings<\/li>\n<li>Create a folder to hold the website<\/li>\n<li>Create the website<\/li>\n<li>Configure a scheduled task to rotate and archive log files.<\/li>\n<\/ol>\n<p>This script is highly customized for my needs and my environment. As you can se there&#8217;s a lot of assumptions in the script, that you may want to change, but i may be a nice start for you!<\/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# Usefull Functions #\r\n#####################\r\n\r\n#Get the next website-id from IIS. You should override this if you use random ids, or if your sites isn't in order!\r\nFunction Get-NextSiteId {\r\n\t$MaxId = Get-Website | Measure-Object -Property ID -Maximum\r\n\treturn $id.Maximum + 1\t\r\n}\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, Should be the same as the name of the website (Eg. Soot.Msb.Se)!\r\n$AppPoolDotNetVersion \t= \"v4.0\"\t\t\t\t\t\t#.NET version for pool\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\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\\Sites\\\"\t#Base folder, where the site should be created. Site folder is created automatically\r\n$SiteId\t\t\t\t\t= Get-NextSiteId\t\t\t\t#Get next free ID, change this manually if needed, eg. if you are using random id.\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$($SiteId)\"\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$($SiteId) C:\\WebSite\\Logs\\Backup W3SVC$($SiteId) *.log 31 true\"\r\n$TaskPath\t\t\t\t= \"LogRotate\"\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 \u2013Name $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#Uncomment these three lines to user AppPoolIdentity\r\n$AppPool.processModel.identityType = 3\r\n$AppPool.ProcessModel.Username = $AppPoolIdentityName \r\n$AppPool.ProcessModel.Password = $AppPoolIdentityPwd\r\n\r\n$AppPool.ProcessModel.IdleTimeout = \"0\"\r\n$AppPool.ManagedRuntimeVersion = $AppPoolDotNetVersion\r\n\r\n#Save and start AppPool\r\n$AppPool | Set-Item\r\n$AppPool | Start-WebAppPool\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 store it in $WebSite\r\nNew-Website -Name $SiteName -PhysicalPath \"$($SiteDirectory)$($SiteName)\" -ApplicationPool $AppPoolName -Port \"170$($SiteId)\" -HostHeader \"localhost\"\r\n$WebSite = Get-Item IIS:\\Sites\\$($SiteName) \r\n\r\n#Start Website\r\n$WebSite | Start-WebSite\r\n\r\n######################################\r\n# Create scheduled job for LogRotate #\r\n# This only works on Win2012         #\r\n######################################\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, are maintaining lots of websites, you probably want to script as much as you can. It&#8217;s time consuming to change a setting for 20+ websites, and it&#8217;s also a great chance you miss something in the process. PowerShell is really neat in these cases, and it gets better for every new &hellip; <a href=\"https:\/\/jonika.nu\/JonasBlogg\/archives\/410\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to create Websites (and more) 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,96,95],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/410"}],"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=410"}],"version-history":[{"count":2,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/410\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/tags?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}