32

Caveat: using one line each!

I had these commands for use in IIS 6, and they worked just fine.

Start:

(get-wmiobject -namespace 'root\MicrosoftIISv2' -computername 'REMOTE_SERVER' -class 'IIsApplicationPool' | where-object {$_.Name -eq 'W3SVC/AppPools/MY_FANCY_APPPOOL'}).InvokeMethod('Stop', $null)"

-and-

Stop:

(get-wmiobject -namespace 'root\MicrosoftIISv2' -computername 'REMOTE_SERVER' -class 'IIsApplicationPool' | where-object {$_.Name -eq 'W3SVC/AppPools/MY_FANCY_APPPOOL'}).InvokeMethod('Start', $null)

I'm looking for an alternative in IIS 8. I need a couple of one-liners and they must be Powershell commands. I'm invoking them via a InvokePowerShellCommand activity in TFS. Is there anyone out there who can help me out?

4
  • 1
    You shouldn't be messing with app pools as part of your build process. Deploy is a separate concern from build -- use a release management tool for this purpose. Overextending the build process to do deployments is generally very painful and inflexible. Commented Mar 12, 2015 at 17:03
  • Absolutely agree with @DanielMann. The object of a "build" job is to produce an a successfully tested release candidate artifact. Commented Oct 2, 2018 at 4:13
  • 1
    Having a build perform deployment steps is not an uncommon practice for CI or CD builds. Though I'm not sure why one needs to recycle the app pools explicitly when saving the web.config file or altering the bin folder forces a recycle.
    – StingyJack
    Commented Nov 11, 2018 at 18:32
  • 3
    You might need to do it if you're deploying an ASP.NET Core app which is hosted using IIS (out-of-process). App files are locked when running, so you'd need to stop the pool, do your deployment and then restart. Commented May 20, 2019 at 14:08

2 Answers 2

53

You can do the following to start your application pool :

Invoke-Command -ComputerName "REMOTE_SERVER" -ScriptBlock { Start-WebAppPool -Name "MY_FANCY_APPPOOL" }

You can do the following to stop your application pool :

Invoke-Command -ComputerName "REMOTE_SERVER" -ScriptBlock { Stop-WebAppPool -Name "MY_FANCY_APPPOOL" }
6
  • 1
    Do you have to Import-Module anything before running Stop-WebAppPool? I've had issues with these commands working even though IIS was installeld.
    – FilBot3
    Commented Feb 4, 2016 at 14:33
  • I can't check it now but I guess you have to enable remote administration of IIS on the REMOTE_SERVER first. Commented Apr 12, 2016 at 8:00
  • 2
    @Pred yes, add this to the beginning of the ScriptBlock: Import-Module WebAdministration;
    – DonBecker
    Commented May 3, 2016 at 2:34
  • 5
    I am getting below exception while using the above command: [XXX.XX.X.XX] Connecting to remote server XXX.XX.X.XX failed with the following error message : The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm...
    – Velmurugan
    Commented May 31, 2016 at 11:53
  • I did not have WebAdministration loaded on my Win10 dev box and I was able to execute this against a Server 2012 R2 lab box without any errors.
    – AWinkle
    Commented Jul 22, 2016 at 14:30
16

To start, sometimes you need to add an explicit wait so that the app pool responds to control messages:

Invoke-Command -ComputerName "$REMOTE_SERVER" -ScriptBlock { Import-Module WebAdministration; Start-Sleep -s 10; Start-WebAppPool -Name "$APP_POOL_NAME" }

And to stop:

Invoke-Command -ComputerName "$REMOTE_SERVER" -ScriptBlock { Import-Module WebAdministration; Stop-WebAppPool -Name "$APP_POOL_NAME" }
1
  • Is there a specific way to inform the script what account (service account?) the powershell should use?
    – Taersious
    Commented Jan 23, 2023 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.