使用Powershell或命令行启动/停止应用程序池IIS6.0

使用Powershell或命令行启动/停止应用程序池IIS6.0,powershell,iis-6,Powershell,Iis 6,我正在使用IIS 6.0并寻找停止/启动应用程序池的方法。我知道powershell在7.0中有一个停止应用程序池,但使用6.0:-(那么是否有人拥有powershell脚本或其他命令行exe来停止/启动应用程序池 谢谢。好的,我只是添加了一个开关来停止应用程序池,否则它会启动,因为启动已经启动的应用程序池没有坏处: param([string]$appPoolName, [switch]$stop) $appPool = get-wmiobject -namespace "root\Micr

我正在使用IIS 6.0并寻找停止/启动应用程序池的方法。我知道powershell在7.0中有一个停止应用程序池,但使用6.0:-(那么是否有人拥有powershell脚本或其他命令行exe来停止/启动应用程序池


谢谢。

好的,我只是添加了一个开关来停止应用程序池,否则它会启动,因为启动已经启动的应用程序池没有坏处:

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()
   }
}

您可能对我开始维护的Powershell库感兴趣:

psDeploy

除此之外,它还有许多用于IIS6自动化的cmdlet,例如Start-IIS6AppPool、New-IIS6Website


我希望它能有所帮助!

您可以创建一个函数来远程停止或启动应用程序池,如下所示:

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")      
    }
}

如果有人正在寻找一个不需要Powershell的纯命令行工具,我会根据这些其他答案中包含的信息来回答。因为最初的问题是专门寻找可能的命令行替代方案,所以我想在这里与大家分享

用法非常简单:

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

和在bitbucket上可用。这可能会为其他人节省几分钟的时间。

如果在Windows Server 2003上使用提供的脚本会更简单

或者根据您的设置(默认为Cscript而不是WScript),只需


当然,如果您希望远程执行此操作,和/或在没有powershell的机器上执行此操作,则可以修改发布的脚本

它使用WMI从VBScript访问和回收应用程序池。使其停止/启动池而不是回收池是一个很小的更改,您只需在相关应用程序池上调用
.stop
.start

脚本的主要内容解释如下:

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
如果要将其集成到某种命令行/批处理工具链中,可以通过调用以下命令在命令行模式下执行VBScript文件:

CScript.exe \NoLogo MyScriptFile.vbs

\nLogo开关删除VBScript解释器启动消息,并使用CScript.exe运行它意味着调用
WScript.Echo
将转到命令行而不是弹出窗口。

看起来您可以从WMI执行此操作,因此您只需将其转换为PowerShell:。
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
CScript.exe \NoLogo MyScriptFile.vbs