30

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?

Barry Gallagher
  • 6,156
  • 4
  • 26
  • 30
  • 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. – Daniel Mann Mar 12 '15 at 17:03
  • Absolutely agree with @DanielMann. The object of a "build" job is to produce an a successfully tested release candidate artifact. – Andrew Gray Oct 02 '18 at 04: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 Nov 11 '18 at 18:32
  • 2
    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. – Mark Embling May 20 '19 at 14:08

2 Answers2

49

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" }
Mathieu Buisson
  • 1,325
  • 10
  • 8
  • 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 Feb 04 '16 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. – Andrzej Martyna Apr 12 '16 at 08:00
  • 2
    @Pred yes, add this to the beginning of the ScriptBlock: Import-Module WebAdministration; – DonBecker May 03 '16 at 02: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 May 31 '16 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 Jul 22 '16 at 14:30
  • I get the same error as @Velmurugan. Any resolution for this? – Shiva Naru Sep 20 '19 at 18:47
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" }
Zachary
  • 486
  • 4
  • 6
  • Is there a specific way to inform the script what account (service account?) the powershell should use? – Taersious Jan 23 '23 at 20:00