การเขียนสคริปต์อัตโนมัติ
Windows Server 2025 มีเครื่องมือบรรทัดคําสั่งหลักสองรายการสําหรับการจัดการ IIS: โมดูล PowerShell WebAdministration และ AppCmd.exe โมดูล WebAdministration แสดงการกําหนดค่า IIS ผ่านไดรฟ์ PowerShell ที่นําทางได้ (IIS:\) และชุด cmdlet ที่หลากหลาย ช่วยให้คุณสร้างและกําหนดค่าไซต์ พูลแอปพลิเคชัน และการผูกทั้งหมดจากบรรทัดคําสั่ง AppCmd.exe นําเสนอทางเลือกที่มีน้ําหนักเบาสําหรับการสืบค้นอย่างรวดเร็วและการดําเนินการสํารองและกู้คืนการกําหนดค่า เครื่องมือเหล่านี้ช่วยให้คุณสามารถทํางาน IIS ประจําโดยอัตโนมัติ ตั้งแต่การจัดเตรียมไซต์จํานวนมากไปจนถึงการรีไซเคิลพูลแอปพลิเคชันตามกําหนดเวลา คุณจึงสามารถจัดการเซิร์ฟเวอร์ได้อย่างสม่ําเสมอและในวงกว้าง
โมดูล WebAdministration จะจัดส่งพร้อมกับ IIS ในวันที่ Windows Server 2025 ซึ่งจะให้:
- ผู้ให้บริการ PowerShell (IIS:) ที่แสดงการกําหนดค่า IIS เป็นไดรฟ์ที่นําทางได้ คล้ายกับรีจิสทรี (HKLM:) หรือระบบไฟล์ (C:)
- ชุดของ cmdlet สําหรับการดําเนินการ IIS ทั่วไป (New-Website, Remove-WebAppPool, Get-WebBinding และอื่นๆ)
เมื่อต้องการทํางานกับโมดูล ให้เรียกใช้รหัสต่อไปนี้:
# Import the module (required before use in a new session)
Import-Module WebAdministration
# Verify it loaded and list available cmdlets
Get-Command -Module WebAdministration
คุณใช้โมดูลกับผู้ให้บริการ IIS ที่ตั้งอยู่ที่ IIS:\Sites ตัวอย่างต่อไปนี้แสดงวิธีการดูข้อมูลเกี่ยวกับ IIS:
# List all websites
Get-ChildItem IIS:\Sites
# List all application pools
Get-ChildItem IIS:\AppPools
# Browse the contents of a specific site
Get-ChildItem 'IIS:\Sites\Default Web Site'
# View all properties of a site object
Get-Item 'IIS:\Sites\Default Web Site' | Select-Object *
รหัสต่อไปนี้แสดงวิธีการสร้างพูลโปรแกรมประยุกต์ใหม่ และกําหนดค่าให้ใช้ .NET CLR รุ่น 4.0 จากนั้นจะสร้างเว็บไซต์ใหม่ที่เรียกว่า ContosoSite และกําหนดพูลแอปพลิเคชันที่สร้างขึ้นใหม่ให้กับไซต์นั้น:
# Create a new application pool
New-WebAppPool -Name "ContosoPool"
# Set the .NET CLR version (use "" for .NET Core / no managed code)
Set-ItemProperty IIS:\AppPools\ContosoPool `
-Name managedRuntimeVersion -Value "v4.0"
# Set identity to ApplicationPoolIdentity (recommended)
Set-ItemProperty IIS:\AppPools\ContosoPool `
-Name processModel.identityType -Value 4
# Create the website
New-Website -Name "ContosoSite" `
-PhysicalPath "C:\inetpub\ContosoSite" `
-HostHeader "contoso.internal" `
-Port 80 `
-ApplicationPool "ContosoPool"
# Confirm the site is running
Get-Website -Name "ContosoSite" | Select-Object Name, State, PhysicalPath
เมื่อสร้างไซต์แล้ว คุณสามารถจัดการได้ด้วย Website cmdlet ของ PowerShell:
# Start and stop a website
Start-Website -Name "ContosoSite"
Stop-Website -Name "ContosoSite"
# Remove a website and its application pool
Stop-Website -Name "ContosoSite"
Remove-Website -Name "ContosoSite"
Remove-WebAppPool -Name "ContosoPool"
คุณสามารถกําหนดการตั้งค่าไซต์ด้วย Set-ItemProperty และ Set-WebConfigurationProperty ตัวอย่างเช่น เมื่อต้องการเปลี่ยนเส้นทางของไซต์ ปิดใช้งานการเรียกดูไดเรกทอรี และเพิ่มรายการเอกสารเริ่มต้น ให้เรียกใช้รหัสต่อไปนี้:
# Change a site's root physical path
Set-ItemProperty 'IIS:\Sites\ContosoSite' `
-Name physicalPath -Value "D:\WebContent\Contoso"
# Disable directory browsing on a site (recommended for production)
Set-WebConfigurationProperty `
-Filter "/system.webServer/directoryBrowse" `
-PSPath "IIS:\Sites\ContosoSite" `
-Name enabled -Value $false
# Add a default document entry
Add-WebConfiguration `
-Filter "//defaultDocument/files" `
-PSPath "IIS:\Sites\ContosoSite" `
-Value @{value = "default.aspx"}
เมื่อต้องการกําหนดค่าการรีไซเคิลพูลแอปพลิเคชันด้วย PowerShell ให้เรียกใช้รหัสต่อไปนี้:
# Schedule a recycle at 03:00 AM daily (replaces any existing schedule entries)
$pool = Get-Item IIS:\AppPools\ContosoPool
$pool.recycling.periodicRestart.schedule.Clear()
$pool.recycling.periodicRestart.schedule.Add("03:00:00")
$pool | Set-Item
# Disable time-based recycling interval (0 = off)
Set-ItemProperty IIS:\AppPools\ContosoPool `
-Name recycling.periodicRestart.time -Value "00:00:00"
# Recycle when private memory exceeds 1 GB
Set-ItemProperty IIS:\AppPools\ContosoPool `
-Name recycling.periodicRestart.privateMemory -Value 1048576 # KB
# Confirm settings
Get-ItemProperty IIS:\AppPools\ContosoPool -Name recycling.periodicRestart
เมื่อต้องการจัดการการผูกด้วย PowerShell ให้เรียกใช้รหัสต่อไปนี้:
# List all bindings for a site
Get-WebBinding -Name "ContosoSite"
# Add an HTTPS binding with SNI enabled (SslFlags 1)
New-WebBinding -Name "ContosoSite" `
-Protocol "https" `
-Port 443 `
-HostHeader "contoso.internal" `
-SslFlags 1
# Assign a certificate to the HTTPS binding
$cert = Get-ChildItem Cert:\LocalMachine\My |
Where-Object { $_.Subject -like "*contoso.internal*" }
$binding = Get-WebBinding -Name "ContosoSite" -Protocol "https"
$binding.AddSslCertificate($cert.Thumbprint, "My")
# Remove a binding
Remove-WebBinding -Name "ContosoSite" -Protocol "http" -Port 80
การกําหนดค่าจํานวนมากด้วย PowerShell
งานการผลิตทั่วไปคือการใช้การตั้งค่าเดียวกันในทุกไซต์ ตัวอย่างเช่น คุณอาจปิดใช้งานการเรียกดูไดเรกทอรีทั่วทั้งเซิร์ฟเวอร์ หรือสร้างกลุ่มไซต์จากไฟล์ CSV
หากต้องการใช้การตั้งค่ากับทุกไซต์ ให้ปรับแต่งโค้ดต่อไปนี้ตามความต้องการของคุณ:
Import-Module WebAdministration
foreach ($site in Get-Website) {
Set-WebConfigurationProperty `
-Filter "/system.webServer/directoryBrowse" `
-PSPath "IIS:\Sites\$($site.Name)" `
-Name enabled -Value $false
Write-Host "Directory browsing disabled: $($site.Name)"
}
คุณยังสามารถจัดสรรหลายไซต์จากไฟล์ CSV (CSV ควรมีคอลัมน์: Name, PhysicalPath, HostHeader, Port, AppPool):
Import-Module WebAdministration
$sites = Import-Csv -Path "C:\Scripts\sites.csv"
foreach ($s in $sites) {
if (-not (Test-Path "IIS:\AppPools\$($s.AppPool)")) {
New-WebAppPool -Name $s.AppPool
}
if (-not (Test-Path $s.PhysicalPath)) {
New-Item -ItemType Directory -Path $s.PhysicalPath | Out-Null
}
New-Website -Name $s.Name `
-PhysicalPath $s.PhysicalPath `
-HostHeader $s.HostHeader `
-Port $s.Port `
-ApplicationPool $s.AppPool
Write-Host "Created: $($s.Name)"
}
การใช้ AppCmd.exe
AppCmd.exe (%SystemRoot%\System32\inetsrv\appcmd.exe) เป็นเครื่องมือการจัดการบรรทัดคําสั่ง IIS ดั้งเดิม มีค่าสําหรับ:
- คิวรีเฉพาะกิจด่วนที่พร้อมท์คําสั่ง
- การดําเนินการสํารองและกู้คืนการกําหนดค่า
- การเขียนสคริปต์ในสภาพแวดล้อมที่ไม่พึงปรารถนาในการโหลดโมดูล PowerShell
AppCmd.exe ใช้ไวยากรณ์ต่อไปนี้:
appcmd <command> <object-type> [options]
ประเภทออบเจ็กต์ทั่วไป ได้แก่ ไซต์, แอป, vdir, apppool, config, backup, request, wp
บล็อกโค้ดต่อไปนี้มีรายการของตัวอย่างคําสั่ง AppCmd:
REM List all sites and their states
%windir%\system32\inetsrv\appcmd list site
REM List all application pools
%windir%\system32\inetsrv\appcmd list apppool
REM List running worker processes
%windir%\system32\inetsrv\appcmd list wp
REM List currently executing HTTP requests
%windir%\system32\inetsrv\appcmd list requests
REM Create a site
%windir%\system32\inetsrv\appcmd add site ^
/name:"ContosoSite" ^
/physicalPath:"C:\inetpub\ContosoSite" ^
/bindings:"http/*:80:contoso.internal"
REM Start and stop a site
%windir%\system32\inetsrv\appcmd start site /site.name:"ContosoSite"
%windir%\system32\inetsrv\appcmd stop site /site.name:"ContosoSite"
REM Recycle an application pool
%windir%\system32\inetsrv\appcmd recycle apppool /apppool.name:"ContosoPool"
REM Set an app pool to No Managed Code (.NET Core)
%windir%\system32\inetsrv\appcmd set apppool ^
/apppool.name:"ContosoPool" /managedRuntimeVersion:""
AppCmd.exe มีกลไกการสํารองข้อมูลในตัวสําหรับไฟล์การกําหนดค่า IIS หลัก: applicationHost.config, redirection.configและ administration.configสร้างการสํารองข้อมูลที่มีชื่อเสมอก่อนที่จะทําการเปลี่ยนแปลงการกําหนดค่าที่สําคัญ บล็อกโค้ดต่อไปนี้แสดงตัวอย่างวิธีการดําเนินการสํารองข้อมูลด้วย AppCmd.exe:
REM Create a named backup
%windir%\system32\inetsrv\appcmd add backup "PreChangeBackup"
REM List all available backups
%windir%\system32\inetsrv\appcmd list backup
REM Restore from a backup
%windir%\system32\inetsrv\appcmd restore backup "PreChangeBackup"
การสํารองข้อมูลจะถูกเก็บไว้ที่: %SystemRoot%\System32\inetsrv\backup<BackupName>\
เมื่อต้องการทําการสํารองข้อมูลด้วย PowerShell ให้ใช้ cmdlet Backup-WebConfiguration เช่น:
# Backup
Backup-WebConfiguration -Name "PreChangeBackup"
# List backups
Get-WebConfigurationBackup
# Restore
Restore-WebConfiguration -Name "PreChangeBackup"
เคล็ดลับ
สร้างการสํารองข้อมูลที่มีชื่อก่อนติดตั้งหรือลบโมดูล IIS การโยกย้ายไซต์ ทําการเปลี่ยนแปลงการกําหนดค่าขนาดใหญ่ หรือการใช้การอัปเดต Windows ที่ส่งผลต่อ IIS
การจัดกําหนดการงานประจําด้วย PowerShell
เมื่อต้องการกําหนดค่าการรีไซเคิลพูลโปรแกรมประยุกต์ทุกคืนเวลา 2:00 น. ให้เรียกใช้รหัส PowerShell ต่อไปนี้:
Register-ScheduledJob -Name "NightlyPoolRecycle" -ScriptBlock {
Import-Module WebAdministration
Restart-WebAppPool -Name "ContosoPool"
Write-EventLog -LogName Application -Source "MSIISAdmin" `
-EventId 1001 -EntryType Information `
-Message "ContosoPool recycled by scheduled task."
} -Trigger (New-JobTrigger -Daily -At "2:00 AM")
เมื่อต้องการกําหนดค่าการตรวจสอบการหมดอายุของใบรับรองที่ให้คําเตือนล่วงหน้า 30 วัน ให้เรียกใช้รหัสต่อไปนี้:
$threshold = (Get-Date).AddDays(30)
Get-ChildItem Cert:\LocalMachine\My |
Where-Object { $_.NotAfter -lt $threshold -and $_.NotAfter -gt (Get-Date) } |
ForEach-Object {
Write-Warning "Certificate '$($_.Subject)' expires $($_.NotAfter.ToString('yyyy-MM-dd'))"
# Add alerting logic here (event log, email, monitoring system)
}
เมื่อต้องการเก็บถาวรแฟ้มบันทึก IIS ที่เก่ากว่า 30 วัน ให้เรียกใช้รหัสต่อไปนี้:
$logPath = "C:\inetpub\logs\LogFiles"
$archivePath = "D:\LogArchives\IIS"
$cutoff = (Get-Date).AddDays(-30)
if (-not (Test-Path $archivePath)) {
New-Item -ItemType Directory -Path $archivePath | Out-Null
}
Get-ChildItem -Path $logPath -Recurse -Filter "*.log" |
Where-Object { $_.LastWriteTime -lt $cutoff } |
ForEach-Object {
$dest = $_.FullName.Replace($logPath, $archivePath)
$destDir = Split-Path $dest -Parent
if (-not (Test-Path $destDir)) {
New-Item -ItemType Directory -Path $destDir | Out-Null
}
Move-Item -Path $_.FullName -Destination $dest
Write-Host "Archived: $($_.FullName)"
}