Web | IIS Website and App Pool Setup

Script Summary:
This PowerShell script automates the configuration of a new IIS (Internet Information Services) website and its associated application pool. It leverages the `WebAdministration` module to streamline setup tasks, ensuring consistency and reducing manual effort. Key operations include:

Application Pool Configuration
- Creates a new App Pool named "MyWebsite Pool"
- Sets idle timeout to 1 hour
- Configures daily recycling at 2:00 AM
- Assigns a specific user identity for the App Pool (secure password input required)

Website Deployment
- Creates the website directory at `D:\Inetpub\wwwroot\MyWebsite\`
- Sets up a new IIS site named **"mywebsite.com"** bound to port 80
- Associates the site with the newly created App Pool

IIS Logging Customization
- Specifies a custom log directory: `D:\Logs\IISLogs`
- Enables local time rollover for logs
- Configures detailed logging fields including client IP, request method, URI, status codes, bytes sent/received, and more

This script is ideal for system administrators looking to automate IIS provisioning with tailored performance and logging settings.

Import-Module "WebAdministration"

# Variables for AppPool and Website
$Var_AppPool='MyWebsite Pool'
$Var_SitePath='D:\Inetpub\wwwroot\MyWebsite\'
$Var_SiteName='mywebsite.com'
$Var_IISLogsPath='D:\Logs\IISLogs'


# Create AppPool

New-Item IIS:\AppPools\$Var_AppPool
#Set IdleTimeout 60min or 1hr
Set-ItemProperty -Path IIS:\AppPools\$Var_AppPool -Name processModel.idleTimeout -Value ("01:00:00")
#Set Recycling Daily 2am
Set-ItemProperty -Path IIS:\AppPools\$Var_AppPool -Name Recycling.periodicRestart.schedule -Value @{value="02:00"}
#Set Periodic Recycling Daily 2am
Set-ItemProperty -Path IIS:\AppPools\$Var_AppPool -Name Recycling.periodicRestart -Value "0"
#Set Identity
#$IdentityUser= $Username + '@' + $AdDomain
$IdentityPwdTxt = Read-Host -Prompt "Enter the password AppPool user" -AsSecureString
$Identity = @{identitytype="SpecificUser";username=$Netbios_User;password=$IdentityPwdTxt}
Set-ItemProperty -Path IIS:\AppPools\$Var_AppPool -name "processModel" -value $Identity
Restart-WebAppPool -Name $Var_AppPool


# Create Website

#Creates Directories
New-Item -Path $Var_SitePath -Type Directory
#Creates Site
New-Item IIS:\Sites\$Var_SiteName -physicalPath $Var_SitePath -bindings @{protocol="http";bindingInformation=":80:"}
Set-ItemProperty IIS:\Sites\$Var_SiteName -name applicationPool -value $Var_AppPool
#Assign AppPool to Site
Set-ItemProperty IIS:\Sites\$Var_SiteName -name applicationPool -value $Var_AppPool
#Set various IIS Logs' properties
#logfile.directory
Set-ItemProperty -Path IIS:\Sites\$Var_SiteName -Name logfile.directory -Value $Var_IISLogsPath
#logfile.localTimeRollover
Set-ItemProperty -Path IIS:\Sites\$Var_SiteName -Name logfile.localTimeRollover -Value $True
#logfile.logExtFileFlags
$LogAddBytesSentRecv="Date,Time,ClientIP,UserName,ServerIP,Method,UriStem,UriQuery,HttpStatus,Win32Status,BytesSent,BytesRecv,TimeTaken,ServerPort,UserAgent,Referer,HttpSubStatus"
Set-ItemProperty -Path IIS:\Sites\$Var_SiteName -Name logfile.logExtFileFlags -Value $LogAddBytesSentRecv
#Report all logfile properties
Get-ItemProperty -Path IIS:\Sites\$Var_SiteName -Name logfile