简体   繁体   English

如何使用powershell脚本启动和停止IIS中的应用程序池

[英]How to start and stop application pool in IIS using powershell script

I want to start and stop application pool in IIS using powershell script.我想使用 powershell 脚本启动和停止 IIS 中的应用程序池。 I had try to write the script but i didn't get this.我曾尝试编写脚本,但我没有得到这个。

You can use this你可以用这个

if your use (PowerShell 2.0) import WebAdministration module如果您使用 (PowerShell 2.0) 导入 WebAdministration 模块

import-module WebAdministration

Please check the state of the application pool before.请先检查应用程序池的状态。 If the application pool is already stopped you get an exception.如果应用程序池已经停止,则会出现异常。

Stop application pool:停止应用程序池:

$applicationPoolName = 'DefaultAppPool'

if((Get-WebAppPoolState -Name $applicationPoolName).Value -ne 'Stopped'){
    Write-Output ('Stopping Application Pool: {0}' -f $applicationPoolName)
    Stop-WebAppPool -Name $applicationPoolName
} 

Start application pool:启动应用程序池:

if((Get-WebAppPoolState -Name $applicationPoolName).Value -ne 'Started'){
    Write-Output ('Starting Application Pool: {0}' -f $applicationPoolName)
    Start-WebAppPool -Name $applicationPoolName
}

Permissions: You have to be a member of the "IIS Admins" group.权限:您必须是“IIS 管理员”组的成员。

These days the IISAdminstration module has mostly superceded WebAdministration.如今,IISAdminstration 模块已基本取代了 WebAdministration。 So if you're on Windows 10 / Server 2016, you can use Get-IISAppPool :因此,如果您使用的是 Windows 10 / Server 2016,则可以使用Get-IISAppPool

(Get-IISAppPool "name").Recycle()

To stop an App Pool using PowerShell use要使用 PowerShell 停止应用程序池,请使用

Stop-WebAppPool -Name YourAppPoolNameHere

And to start the App Pool并启动应用程序池

Start-WebAppPool -Name YourAppPoolNameHere

You will need the WebAdministration module installed so check you have it with this command您将需要安装WebAdministration模块,因此请使用此命令检查您是否拥有它

 Get-Module -ListAvailable

I use the following code in an Azure pipeline:我在 Azure 管道中使用以下代码:

Stop the pool:停止池:

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;
}

Start the pool:启动池:

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;
}

The variables $WasStarted and $WasStopped are extra information not used within this examples but could be used to determine whether an application pool should be restarted after the deployment of a new version is completed or not (because it was already stopped before).变量$WasStarted$WasStopped是本示例中未使用的额外信息,但可用于确定应用程序池是否应在新版本部署完成后重新启动(因为它之前已停止)。

您必须使用Import-Module导入WebAdministration模块,然后才能使用Start-WebAppPoolStop-WebAppPool

You can stop and stop all application pools respectively using the following powershell script.您可以使用以下 powershell 脚本分别停止和停止所有应用程序池。 The second line below elevates permissions.下面的第二行提升权限。 You could exclude this and just run as administrator.您可以排除它并以管理员身份运行。

Stop all application pools script停止所有应用程序池脚本

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)
}

Start all application pools script启动所有应用程序池脚本

  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)
    }

From microsoft doc.来自微软文档。 https://docs.microsoft.com/en-us/powershell/module/webadminstration/restart-webapppool?view=winserver2012-ps https://docs.microsoft.com/en-us/powershell/module/webadminstration/restart-webapppool?view=winserver2012-ps

Restart-WebAppPool recycles an application pool. Restart-WebAppPool 回收应用程序池。
Then you don't have to think of a stop, wait, and start.那么您就不必考虑停止、等待和开始。

Import-Module WebAdministration

For a specific running AppPool对于特定运行的 AppPool

$applicationPoolName = 'DefaultAppPool'
Get-ChildItem IIS:\AppPools | Where {$_.State -ne "Stopped" -and $_.name -eq $applicationPoolName} | Restart-WebAppPool

For all running AppPools对于所有正在运行的 AppPools

Get-ChildItem IIS:\AppPools | Where {$_.State -ne "Stopped"} | Restart-WebAppPool

Like mentioned by Mitch Pomery in the comments, the apppool does not stop instantly.正如 Mitch Pomery 在评论中提到的那样,apppool 不会立即停止。 Because of this, my CI script failes to copy files into the directory that was still being used by the apppool.因此,我的 CI 脚本无法将文件复制到 apppool 仍在使用的目录中。 Instead, I reverted back to appcmd tool (which apparently waits for the apppool to actually stop), but still used WebAdministration module to check if the pool is running.相反,我恢复到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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM