28

Is it possible to recycle an IIS7 app pool from the command line, on a different machine?

I've found APPCMD (appcmd recycle apppool my-app-pool), but it only operates on the host it's run on, AFAICT.

I heard a rumor there might be a way to do it with Powershell, but I know nothing about that, and I'm apparently not very good at googling for it.

I'm using Vista / Server 2008, if that matters.

EDIT: I found something called WinRM that somebody claims is able to run APPCMD itself, but I'm not sure exactly how, yet.

6 Answers 6

30

Ken, if this is run from CMD you could do it with PSExec. This requires no installation on the server you want to access.

Simply copy the contents of the zip to your System32 file (don't include the ELUA or the DLL file) of the computer you want to remote FROM.
Then enter CMD
type
Psexec \\{Computer Name of ISS7 Server} %systemroot%\System32\inetsrv\appcmd recycle apppool my-app-pool

If you are not on the same domain on an domain admin account you will need to enter credentials for administrative access. For more information simply type psexec in a command line with no arguments.

2
  • 4
    AppCmd.exe location (%systemroot%\system32\inetsrv) is not part of the PATH automatically. You can either add it to your path or run appcmd with: "%systemroot%\system32\inetsrv\AppCmd.exe"
    – ashtonium
    Jan 29, 2013 at 17:17
  • 1
    You don't quote the remote command for PsExec; you send in the command name followed by the arguments all separated by spaces. So command should be be: Psexec \\computer c:\windows\system32\inetsrv\appcmd.exe" recycle apppool AppPoolName
    – Carl G
    Oct 17, 2013 at 16:23
6

To do it remotely from PowerShell you need to either use PowerShell remoting, or use WMI.

It's actually very simple using the Invoke-WMIMethod PowerShell cmdlet, but you have to specify -Authentication PacketPrivacy ... and if you need to specify different credentials, you can do that with parameter -Credential (Get-Credential) ... here's an example:

$pc = "serverName"

## List the app pools, note the __RELPATH of the one you want to kill:
Get-WMIObject IISApplicationPool 
              -Computer $pc 
              -Namespace root\MicrosoftIISv2 
              -Authentication PacketPrivacy 

## Recycle a specific one:
$Name = "W3SVC/APPPOOLS/ASP.NET v4.0 Classic"  ## This is the Name from above
$Path = "IISApplicationPool.Name='$Name'"      ## This is the __RELPATH

Invoke-WMIMethod Recycle 
                 -Path $Path 
                 -Computer $pc 
                 -Namespace root\MicrosoftIISv2 
                 -Authentication PacketPrivacy

I wrote a nice function to wrap all that up: http://poshcode.org/2466

1
3

If command line is your choise, PsExec seems to be the solution, but for mission critical tasks I wouldn't recommend it. The reason is the following: PsExec has problems with multiple runs.

I prefer to use API for such purposes.

  1. WMI will do the job as it is described here (my favourite solution).
  2. Alternatively you can use ServerManager API. It allows to manage application pools on remote machine if you run your code with corresponding credentials.
2

Similar to @Jeff's answer, with Powershell remoting it would be:

Enter-PSSession -ComputerName <your server> -Credential <username, or (Get-Credential)>

Then in the interactive session, then:

appcmd recycle apppool my-app-pool

to restart the app pool.

0

Recycle AppPool in PowerShell

In this example: MSExchangeSyncAppPool

$appPool = Get-WmiObject -Authentication PacketPrivacy -Impersonation Impersonate -ComputerName (hostname) -namespace "root/MicrosoftIISv2" -class IIsApplicationPool | Where-Object {$_.Name -eq "W3SVC/AppPools/MSExchangeSyncAppPool" }
$appPool.Recycle()
0

For completeness, there is also ADSI Service Provider for IIS (Warning: Outdated technology):

$ComputerName = 'webserver01.local'
$AppPool = 'MyWebPortalPool'

# Load System.DirectoryServices assembly if not already loaded:
if (-not ([System.Management.Automation.PSTypeName]'System.DirectoryServices.DirectoryEntry').Type) {
    Add-Type -AssemblyName System.DirectoryServices
}

$PoolPath = "IIS://$ComputerName/W3SVC/AppPools/$AppPool"

$PoolObj = New-Object System.DirectoryServices.DirectoryEntry($PoolPath)
$PoolObj.Invoke('Recycle', $null)

$PoolObj.Dispose()

You must log in to answer this question.