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

54

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" } 
Sign up to request clarification or add additional context in comments.

6 Comments

Do you have to Import-Module anything before running Stop-WebAppPool? I've had issues with these commands working even though IIS was installeld.
I can't check it now but I guess you have to enable remote administration of IIS on the REMOTE_SERVER first.
@Pred yes, add this to the beginning of the ScriptBlock: Import-Module WebAdministration;
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...
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.
|
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 Comment

Is there a specific way to inform the script what account (service account?) the powershell should use?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.