Skip to content

Mustafa Ateş UZUN Blog

mustafauzun.co

  • About
  • Blog
  • Home
  • »
  • Powershell
  • »
  • Powershell, IIS Website Installer Script
  • »
    • May 23, 2023
    • by mustafauzun
  • Powershell

Powershell, IIS Website Installer Script

Write-Host "[Website IIS Installer]"

$informationTexts = @{
    Success = @{Text = "Success"; BackgroundColor = "DarkGreen"}
    Error = @{Text = "Error"; BackgroundColor = "DarkRed"}
    Warning = @{Text="Warning"; BackgroundColor = "DarkCyan"}
}



Write-Host "Please Run Powershell as 64bit" -NoNewLine
Write-Host " - " -NoNewLine
if ([Environment]::Is64BitProcess -eq [Environment]::Is64BitOperatingSystem)
{
  Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor
}
else
{
    Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor
    exit
}

$websiteDefaultName = "website.local"
$websiteName = Read-Host -Prompt "Input website website name (default: $websiteDefaultName)"

if(-not($websiteName))
{
    $websiteName = $websiteDefaultName
}

$iisDefaultName = "website.local"
$iisName = Read-Host -Prompt "Input website iis name (default: $iisDefaultName)"

if(-not($iisName))
{
    $iisName = $iisDefaultName
}

$iisDefaultFolderPath = "$((get-location).Drive.Name):\Web"
$iisFolderPath = Read-Host -Prompt "Input website folder path (default: $iisDefaultFolderPath)"

if(-not($iisFolderPath))
{
    $iisFolderPath = $iisDefaultFolderPath
}

Import-Module WebAdministration

Write-Host "Adding Website Paths on IIS Application Pool" -NoNewLine
Write-Host " - " -NoNewLine

try
{
    $iisPath = $("IIS:\AppPools\" + $iisName)
    New-Item -Path $iisPath -Force > $null
    Set-ItemProperty -Path $iisPath -Name managedRuntimeVersion -Value "v4.0"
    Set-ItemProperty -Path $iisPath -Name enable32BitAppOnWin64 -Value $true

    Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor
}
catch
{
    Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor
}


Write-Host "Adding Website on ISS" -NoNewLine
Write-Host " - " -NoNewLine

$rootPath = "websiteUI"
$virtualDirectoryPaths = @("websiteFolder1", "websiteFolder2", "websiteFolder3")

try
{
    New-WebSite -Name $iisName -Port 80 -HostHeader $websiteName -PhysicalPath $($iisFolderPath + "\" + $iisName + "\" + $rootPath) -ApplicationPool $iisName -Force > $null
    New-WebBinding -Name $iisName -IPAddress "*" -Port 443 -HostHeader $websiteName -Protocol https > $null

    Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor -NoNewLine
    Write-Host " - " -NoNewLine
    Write-Host "Do not forget to define an SSL certificate for 443 port" -BackgroundColor $informationTexts.Warning.BackgroundColor
}
catch
{
    Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor
}

foreach($virtualDirectoryPath in $virtualDirectoryPaths)
{
    Write-Host "Adding Virtual Directory" -NoNewLine
    Write-Host " - " -NoNewLine
    Write-Host $virtualDirectoryPath -NoNewLine
    Write-Host " - " -NoNewLine
    
    try
    {
        New-WebApplication -Name $virtualDirectoryPath -Site $iisName -PhysicalPath $($iisFolderPath + "\" + $iisName + "\" + $virtualDirectoryPath) -ApplicationPool $iisName > $null

        Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor
    }
    catch
    {
        Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor
    }
}

Write-Host "Starting Application Pool" -NoNewLine
Write-Host " - " -NoNewLine

try
{
    Start-WebAppPool -Name $iisName

    if((Get-WebAppPoolState -Name $iisName).Value -eq "Started")
    {
        Write-Host $informationTexts.Success.Text -BackgroundColor $informationTexts.Success.BackgroundColor
    }
    else
    {
        throw
    }
}
catch
{
    Write-Host $informationTexts.Error.Text -BackgroundColor $informationTexts.Error.BackgroundColor
}
Share

Powershell, Host Path Checker Script

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Javascript, Task Queue
  • Javascript, Thread Pool
  • Javascript, Lexical Environment
  • Javascript, Execution Context
  • Javascript, Event Delegation

Recent Comments

No comments to show.

Archives

  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • January 2022

Categories

  • C#
  • CMD
  • CSS
  • HTML
  • JavaScript
  • MongoDB
  • Node.js
  • Podcast
  • Powershell
  • Uncategorized
Theme: Puskar by Template Sell.