Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 556 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to start and stop application pool in IIS using powershell script

#1
I want to start and stop application pool in IIS using powershell script. I had try to write the script but i didn't get this.
Reply

#2
You have to import the `WebAdministration` module using [Import-Module][1] and then you can use [Start-WebAppPool][2] and [Stop-WebAppPool][3]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#3
To stop an App Pool using PowerShell use

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

Get-Module -ListAvailable

Reply

#4
You can use this

**if your use (PowerShell 2.0) import WebAdministration module**

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.**
Reply

#5
You can stop and stop all application pools respectively using the following powershell script. 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)
}

Reply

#6
From microsoft doc. [

[To see links please register here]

][1]

Restart-WebAppPool recycles an application pool.
Then you don't have to think of a stop, wait, and start.

Import-Module WebAdministration

For a specific running AppPool

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

For all running AppPools

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

[1]:

[To see links please register here]

Reply

#7
I use the following code in an Azure pipeline:

**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).
Reply

#8
Like mentioned by Mitch Pomery in the comments, the apppool does not stop instantly. Because of this, my CI script failes to copy files into the directory that was still being used by the 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.

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"

Reply

#9
These days the IISAdminstration module has mostly superceded WebAdministration.

So if you're on Windows 10 / Server 2016, you can use [Get-IISAppPool][1] like this:

```ps1
Import-Module IISAdministration
(Get-IISAppPool "name").Recycle()
```

[1]:

[To see links please register here]

Reply

#10
I ended up with something like this:

**Stopping the app pool:**

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

**Starting the app pool:**

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through