Administrators running .NET workloads on Windows Server benefit from replacing manual IIS Manager steps with repeatable PowerShell commands. This approach reduces configuration drift and speeds up deployment across multiple hosts.
The examples below target current Windows Server releases and the latest stable IIS PowerShell module. They focus on site creation, application pool settings, and Active Directory authentication.
#Importing the IIS Module and Verifying the Environment
Begin every script by loading the WebAdministration module. The following commands confirm the module is present and list existing sites.
Import-Module WebAdministration
Get-Website | Select-Object Name, State, PhysicalPath
#Creating a Site and Application Pool
Use New-WebAppPool and New-Website to create isolated pools with explicit .NET settings. Set the managed runtime to No Managed Code when hosting ASP.NET Core applications.
New-WebAppPool -Name "MyAppPool"
Set-ItemProperty IIS:\AppPools\MyAppPool -Name managedRuntimeVersion -Value ""
New-Website -Name "MySite" -PhysicalPath "C:\inetpub\mysite" -ApplicationPool "MyAppPool" -BindingInformation "*:80:"
#Enforcing Active Directory Authentication
- Enable Windows authentication at the site level.
- Disable anonymous authentication to require domain credentials.
- Configure the application pool identity to a domain service account.
Set-WebConfigurationProperty -Filter /system.webServer/security/authentication/windowsAuthentication -Name enabled -Value $true -PSPath IIS:\Sites\MySite
Set-WebConfigurationProperty -Filter /system.webServer/security/authentication/anonymousAuthentication -Name enabled -Value $false -PSPath IIS:\Sites\MySite
#Applying Request Limits and Logging
Limit request sizes and enable detailed logging for troubleshooting. These settings help prevent resource exhaustion on shared hosts.
Set-WebConfigurationProperty -Filter system.webServer/security/requestFiltering/requestLimits -Name maxAllowedContentLength -Value 30000000 -PSPath IIS:\Sites\MySite
Set-ItemProperty IIS:\Sites\MySite -Name logFile.directory -Value "C:\logs\iis"
Run these scripts from an elevated PowerShell session on the target server. Store service account credentials in a secure vault rather than embedding them in scripts.
Comments
No comments yet