![](/img/trans.png)
[英]Is it possible to start / stop an application pool or website in IIS programatically?
[英]How to start and stop application pool in IIS using powershell script
我想使用 powershell 腳本啟動和停止 IIS 中的應用程序池。 我曾嘗試編寫腳本,但我沒有得到這個。
你可以用這個
如果您使用 (PowerShell 2.0) 導入 WebAdministration 模塊
import-module WebAdministration
請先檢查應用程序池的狀態。 如果應用程序池已經停止,則會出現異常。
停止應用程序池:
$applicationPoolName = 'DefaultAppPool'
if((Get-WebAppPoolState -Name $applicationPoolName).Value -ne 'Stopped'){
Write-Output ('Stopping Application Pool: {0}' -f $applicationPoolName)
Stop-WebAppPool -Name $applicationPoolName
}
啟動應用程序池:
if((Get-WebAppPoolState -Name $applicationPoolName).Value -ne 'Started'){
Write-Output ('Starting Application Pool: {0}' -f $applicationPoolName)
Start-WebAppPool -Name $applicationPoolName
}
權限:您必須是“IIS 管理員”組的成員。
如今,IISAdminstration 模塊已基本取代了 WebAdministration。 因此,如果您使用的是 Windows 10 / Server 2016,則可以使用Get-IISAppPool :
(Get-IISAppPool "name").Recycle()
要使用 PowerShell 停止應用程序池,請使用
Stop-WebAppPool -Name YourAppPoolNameHere
並啟動應用程序池
Start-WebAppPool -Name YourAppPoolNameHere
您將需要安裝WebAdministration
模塊,因此請使用此命令檢查您是否擁有它
Get-Module -ListAvailable
我在 Azure 管道中使用以下代碼:
停止池:
Import-Module -Name 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\WebAdministration.psd1';
$AppPoolName = 'DefaultAppPool';
$AppPoolState = (Get-WebAppPoolState -Name $AppPoolName).Value;
$WasStarted = $false;
$Timeout = [System.TimeSpan]::FromMinutes(1);
$StopWatch = New-Object -TypeName 'System.Diagnostics.Stopwatch';
$StopWatch.Start();
# Possible status: "Starting", "Started", "Stopping", "Stopped" and "Unknown".
while ($AppPoolState -ne 'Stopped') {
if ($AppPoolState -eq 'Started') {
$WasStarted = $true;
Stop-WebAppPool -Name $AppPoolName;
}
Start-Sleep -Seconds 2;
if ($StopWatch.Elapsed -gt $Timeout) {
throw New-Object -TypeName 'System.TimeoutException' -ArgumentList "Timeout of $($Timeout.TotalSeconds) seconds exceeded!";
}
$AppPoolState = (Get-WebAppPoolState -Name $AppPoolName).Value;
}
啟動池:
Import-Module -Name 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration\WebAdministration.psd1';
$AppPoolName = 'DefaultAppPool';
$AppPoolState = (Get-WebAppPoolState -Name $AppPoolName).Value;
$WasStopped = $false;
$Timeout = [System.TimeSpan]::FromMinutes(1);
$StopWatch = New-Object -TypeName 'System.Diagnostics.Stopwatch';
$StopWatch.Start();
# Possible status: "Starting", "Started", "Stopping", "Stopped" and "Unknown".
while ($AppPoolState -ne 'Started') {
if ($AppPoolState -eq 'Stopped') {
$WasStopped = $true;
Start-WebAppPool -Name $AppPoolName;
}
Start-Sleep -Seconds 2;
if ($StopWatch.Elapsed -gt $Timeout) {
throw New-Object -TypeName 'System.TimeoutException' -ArgumentList "Timeout of $($Timeout.TotalSeconds) seconds exceeded!";
}
$AppPoolState = (Get-WebAppPoolState -Name $AppPoolName).Value;
}
變量$WasStarted
和$WasStopped
是本示例中未使用的額外信息,但可用於確定應用程序池是否應在新版本部署完成后重新啟動(因為它之前已停止)。
您必須使用Import-Module導入WebAdministration
模塊,然后才能使用Start-WebAppPool和Stop-WebAppPool
您可以使用以下 powershell 腳本分別停止和停止所有應用程序池。 下面的第二行提升權限。 您可以排除它並以管理員身份運行。
停止所有應用程序池腳本
Import-Module WebAdministration
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
$AppPools=Get-ChildItem IIS:\AppPools | Where {$_.State -eq "Started"}
ForEach($AppPool in $AppPools)
{
Stop-WebAppPool -name $AppPool.name
# Write-Output ('Stopping Application Pool: {0}' -f $AppPool.name)
}
啟動所有應用程序池腳本
Import-Module WebAdministration
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
$AppPools=Get-ChildItem IIS:\AppPools | Where {$_.State -eq "Stopped"}
ForEach($AppPool in $AppPools)
{
Start-WebAppPool -name $AppPool.name
# Write-Output ('Starting Application Pool: {0}' -f $AppPool.name)
}
Restart-WebAppPool 回收應用程序池。
那么您就不必考慮停止、等待和開始。
Import-Module WebAdministration
對於特定運行的 AppPool
$applicationPoolName = 'DefaultAppPool'
Get-ChildItem IIS:\AppPools | Where {$_.State -ne "Stopped" -and $_.name -eq $applicationPoolName} | Restart-WebAppPool
對於所有正在運行的 AppPools
Get-ChildItem IIS:\AppPools | Where {$_.State -ne "Stopped"} | Restart-WebAppPool
正如 Mitch Pomery 在評論中提到的那樣,apppool 不會立即停止。 因此,我的 CI 腳本無法將文件復制到 apppool 仍在使用的目錄中。 相反,我恢復到appcmd
工具(它顯然在等待 apppool 實際停止),但仍然使用WebAdministration
模塊來檢查池是否正在運行。
Import-Module WebAdministration
if ((Get-WebAppPoolState -Name PCSServer).Value -ne 'Stopped'){ C:\Windows\system32\inetsrv\appcmd stop apppool "PCSServer-WS" }
Expand-Archive -Path MBE.zip -DestinationPath $DEPLOY_PATH -Force
C:\Windows\system32\inetsrv\appcmd start apppool "PCSServer-WS"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.