简体   繁体   中英

Start/Stop App Pool IIS6.0 with Powershell or command line

I'm using IIS 6.0 and looking for a way to stop/start the app pool. I know there is a stop-appPool for powershell in 7.0 but using 6.0. :-( So does anyone have a powershell script or another command line exe that will stop/start the app pool?

Thanks.

Ok here it is, I just add a switch to stop the app pool else it starts since no harm in starting an app pool that is already started:

param([string]$appPoolName, [switch]$stop)

$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | where-object {$_.Name -eq "W3SVC/AppPools/$appPoolName"}

if($appPool)
{
   if($stop)
   {
      $appPool.Stop()
   }
   else
   {
      $appPool.Start()
   }
}

If anybody is looking for a purely command-line tool that does not require Powershell, I have created such a thing based on the information contained in these other answers. Since the original question is specifically looking for possible command-line alternatives, I thought I would share it here.

Usage is quite simple:

IIS6AppPool Start DefaultAppPool
IIS6AppPool Stop AppPool #1
IIS6AppPool Recycle Some other app pool

Source and binaries are available on bitbucket. May this save somebody else a few minutes of head scratching.

If on Windows Server 2003 it is simpler to use the supplied script iisapp.vbs

CScript.exe C:\WINDOWS\system32\iisapp.vbs /?
CScript.exe C:\WINDOWS\system32\iisapp.vbs /a MyApp /r

Or depending on your setup (default to Cscript not WScript), simply

iisapp /a MyApp /r

And of course it is different in IIS7

You might be interested in this Powershell library I started maintaining:

psDeploy : http://rprieto.github.com/psDeploy/

Among other things it has lots of cmdlets for IIS6 automation, for example Start-IIS6AppPool , New-IIS6Website ...

I hope it helps!

If you wish to do this remotely, and / or on a machine without powershell you can modify the script posted here .

It uses WMI to access and recycle the app pool, from VBScript. It's a trivial change to make it stop / start pools instead of recycling them, you just need to call .Stop or .Start on the app pool in question.

The meat of the script is paraphrased below:

strServer = "LocalHost" 'Server name goes here
strAppPoolName = "MyAppPool" 'App pool name goes here

'Connect to the specified server using WMI
set Locator = CreateObject("WbemScripting.SWbemLocator")
Locator.Security_.AuthenticationLevel = 6
set Service = locator.connectserver(strServer,"root/MicrosoftIISv2")

'Get a collection of WMI apppools
set APCollection = Service.InstancesOf("IISApplicationPool")

For each APInstance in APCollection
    If UCase(ApInstance.Name) = UCase("W3SVC/AppPools/" & strAppPoolName) Then
        WScript.Echo "Recycling " & strServer & "/" & APInstance.Name
            ' You can do any of these things depending you what you want to do.
            APInstance.Recycle
            APInstance.Stop
            APInstance.Start
        End If
    Next

If you have some kind of command line / batch toolchain which you want to integrate this into, you can execute a VBScript file in command line mode by calling:

CScript.exe \NoLogo MyScriptFile.vbs

The \\NoLogo switch removes the VBScript interpreter startup messages and running it with CScript.exe means that calls to WScript.Echo go to the command line rather than a popup window.

You could create a function to stop or start the application pool remotely as below:

function StopOrStartAppPool($RemoteServerName, $AppPoolName, $commandWebPool)
{  

    if ($commandWebPool -eq "Stop")
    { 
       $wmiprocess = [wmiclass]"\\$RemoteServerName\root\cimv2:win32_process"
       $wmiprocess.create("cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs STOP_SERVER W3SVC/AppPools/$AppPoolName -s:$RemoteServerName")  
    }
    else
    {
       $wmiprocess = [wmiclass] "\\$RemoteServerName\root\cimv2:win32_process"
       $wmiprocess.create("cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs START_SERVER W3SVC/AppPools/$AppPoolName -s:$RemoteServerName")      
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM