我想使用PowerShell脚本启动和停止IIS中的应用程序池。 我尝试编写脚本,但未成功。
我想使用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管理员"组的成员。
现在,IIS管理模块在很大程度上已经取代了WebAdministration。
因此,如果您使用的是Windows 10 / Server 2016,则可以像这样使用Get-IISAppPool:
Import-Module IISAdministration
(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
是额外信息,在此示例中未使用,但可用于确定应用程序池在部署新版本完成后是否应重新启动(因为它已被停止)。您可以使用以下 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)
}
Get-IISSite | Select-Object Name | Start-IISSite
和 Get-IISSite | Select-Object Name | Stop-IISSite
。 - Blue Eyed Behemoth - task: PowerShell@2
displayName: Stop App Pool
inputs:
targetType: 'inline'
script: |
$applicationPoolName = '$(MySite)AppPool'
if((Get-WebAppPoolState -Name $applicationPoolName).Value -ne 'Stopped')
{
Write-Output ('Stopping Application Pool: {0}' -f $applicationPoolName)
Stop-WebAppPool -Name $applicationPoolName
Start-Sleep -Seconds 2
}
启动应用程序池:
- task: PowerShell@2
displayName: Start App Pool
inputs:
targetType: 'inline'
script: |
$applicationPoolName = '$(MySite)AppPool'
if((Get-WebAppPoolState -Name $applicationPoolName).Value -ne 'Started')
{
Start-Sleep -Seconds 2
Write-Output ('Starting Application Pool: {0}' -f $applicationPoolName)
Start-WebAppPool -Name $applicationPoolName
}
正如Mitch Pomery在评论中提到的那样,应用程序池不会立即停止。因此,我的CI脚本无法将文件复制到仍在被应用程序池使用的目录中。
相反,我回退到了appcmd
工具(显然等待应用程序池实际停止),但仍然使用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"
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