{"id":1046,"date":"2023-05-23T08:48:23","date_gmt":"2023-05-23T08:48:23","guid":{"rendered":"https:\/\/mustafauzun.co\/blog\/?p=1046"},"modified":"2023-05-23T08:48:23","modified_gmt":"2023-05-23T08:48:23","slug":"powershell-iis-website-installer-script","status":"publish","type":"post","link":"https:\/\/mustafauzun.co\/blog\/powershell-iis-website-installer-script\/","title":{"rendered":"Powershell, IIS Website Installer Script"},"content":{"rendered":"\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"Install.ps1\"><code>Write-Host &quot;[Website IIS Installer]&quot;\n\n$informationTexts = @{\n    Success = @{Text = &quot;Success&quot;; BackgroundColor = &quot;DarkGreen&quot;}\n    Error = @{Text = &quot;Error&quot;; BackgroundColor = &quot;DarkRed&quot;}\n    Warning = @{Text=&quot;Warning&quot;; BackgroundColor = &quot;DarkCyan&quot;}\n}\n\n\n\nWrite-Host &quot;Please Run Powershell as 64bit&quot; -NoNewLine\nWrite-Host &quot; - &quot; -NoNewLine\nif ([Environment]::Is64BitProcess -eq [Environment]::Is64BitOperatingSystem)\n{\n  Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor\n}\nelse\n{\n    Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor\n    exit\n}\n\n$websiteDefaultName = &quot;website.local&quot;\n$websiteName = Read-Host -Prompt &quot;Input website website name (default: $websiteDefaultName)&quot;\n\nif(-not($websiteName))\n{\n    $websiteName = $websiteDefaultName\n}\n\n$iisDefaultName = &quot;website.local&quot;\n$iisName = Read-Host -Prompt &quot;Input website iis name (default: $iisDefaultName)&quot;\n\nif(-not($iisName))\n{\n    $iisName = $iisDefaultName\n}\n\n$iisDefaultFolderPath = &quot;$((get-location).Drive.Name):\\Web&quot;\n$iisFolderPath = Read-Host -Prompt &quot;Input website folder path (default: $iisDefaultFolderPath)&quot;\n\nif(-not($iisFolderPath))\n{\n    $iisFolderPath = $iisDefaultFolderPath\n}\n\nImport-Module WebAdministration\n\nWrite-Host &quot;Adding Website Paths on IIS Application Pool&quot; -NoNewLine\nWrite-Host &quot; - &quot; -NoNewLine\n\ntry\n{\n    $iisPath = $(&quot;IIS:\\AppPools\\&quot; + $iisName)\n    New-Item -Path $iisPath -Force &gt; $null\n    Set-ItemProperty -Path $iisPath -Name managedRuntimeVersion -Value &quot;v4.0&quot;\n    Set-ItemProperty -Path $iisPath -Name enable32BitAppOnWin64 -Value $true\n\n    Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor\n}\ncatch\n{\n    Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor\n}\n\n\nWrite-Host &quot;Adding Website on ISS&quot; -NoNewLine\nWrite-Host &quot; - &quot; -NoNewLine\n\n$rootPath = &quot;websiteUI&quot;\n$virtualDirectoryPaths = @(&quot;websiteFolder1&quot;, &quot;websiteFolder2&quot;, &quot;websiteFolder3&quot;)\n\ntry\n{\n    New-WebSite -Name $iisName -Port 80 -HostHeader $websiteName -PhysicalPath $($iisFolderPath + &quot;\\&quot; + $iisName + &quot;\\&quot; + $rootPath) -ApplicationPool $iisName -Force &gt; $null\n    New-WebBinding -Name $iisName -IPAddress &quot;*&quot; -Port 443 -HostHeader $websiteName -Protocol https &gt; $null\n\n    Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor -NoNewLine\n    Write-Host &quot; - &quot; -NoNewLine\n    Write-Host &quot;Do not forget to define an SSL certificate for 443 port&quot; -BackgroundColor $informationTexts.Warning.BackgroundColor\n}\ncatch\n{\n    Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor\n}\n\nforeach($virtualDirectoryPath in $virtualDirectoryPaths)\n{\n    Write-Host &quot;Adding Virtual Directory&quot; -NoNewLine\n    Write-Host &quot; - &quot; -NoNewLine\n    Write-Host $virtualDirectoryPath -NoNewLine\n    Write-Host &quot; - &quot; -NoNewLine\n    \n    try\n    {\n        New-WebApplication -Name $virtualDirectoryPath -Site $iisName -PhysicalPath $($iisFolderPath + &quot;\\&quot; + $iisName + &quot;\\&quot; + $virtualDirectoryPath) -ApplicationPool $iisName &gt; $null\n\n        Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor\n    }\n    catch\n    {\n        Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor\n    }\n}\n\nWrite-Host &quot;Starting Application Pool&quot; -NoNewLine\nWrite-Host &quot; - &quot; -NoNewLine\n\ntry\n{\n    Start-WebAppPool -Name $iisName\n\n    if((Get-WebAppPoolState -Name $iisName).Value -eq &quot;Started&quot;)\n    {\n        Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor\n    }\n    else\n    {\n        throw\n    }\n}\ncatch\n{\n    Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor\n}<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":1034,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-1046","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/1046","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/comments?post=1046"}],"version-history":[{"count":1,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/1046\/revisions"}],"predecessor-version":[{"id":1047,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/1046\/revisions\/1047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media\/1034"}],"wp:attachment":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media?parent=1046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/categories?post=1046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/tags?post=1046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}