iis - 使用 iisapp.vbs 启动 MyAppPool

标签 iis batch-file iis-6 application-pool

我有一个批处理文件,它使用以下脚本来回收 MyAppPool

cscript.exe %windir%\system32\iisapp.vbs /a MyAppPool /r

但是,当 MyAppPool 停止时,我无法回收它。我想要的是检查天气 MyAppPool 是否已停止,如果已停止,则启动它,回收它,然后再次停止。

嗯,我在 IIS 方面完全是个新手,从来没有从事过它的工作。我使用的是 Window Server 2003 和 IIS6。

最佳答案

您可以编写自己的 .vbs 脚本,以查找 AppPool 的 .State,并在其停止时启动它。像这样的东西:

//编辑:

Option Explicit

If WScript.Arguments.Count <> 1 Then
    Wscript.Echo "No AppPoolName provided. Iterate through all AppPools"
    iterate_and_start_all_apps()
Else
    Dim AppPoolName
    AppPoolName = Wscript.Arguments(0)
    '
    ' Choose what to do here and uncomment that Sub
    '
    ' start_given_app(AppPoolName)
    ' start_one_app_if_stopped(AppPoolName)
    ' start_recycle_stop_app(AppPoolName)
End If

' This Sub is runs if no argument is passed to the script
Sub iterate_and_start_all_apps()
        Dim objAppPools, objAppPool
        Set objAppPools = GetObject("IIS://Localhost/W3SVC/AppPools")
        For Each objAppPool in objAppPools
          Set objAppPool = GetObject("IIS://Localhost/W3SVC/AppPools/" & objAppPool.Name )
            If objAppPool.AppPoolState <> 2 Then
              Wscript.Echo objAppPool.Name & " is not running."
                  WScript.Echo objAppPool.Name & ", AppPoolState: " & objAppPool.AppPoolState & _
                        ", Win32Error: " & objAppPool.Win32Error & " ("& hex(objAppPool.Win32Error)&")"
                  Wscript.Echo State2Desc(objAppPool.AppPoolState)
                  objAppPool.Start
                  If Err.Number = 0 Then
                    Wscript.Echo objAppPool.Name & " started."
                  End If
                End If
        Next
        Set objAppPool = Nothing
        Set objAppPools = Nothing
End Sub

'
' start an application pool if the .State is stopped
'
Sub start_one_app_if_stopped(applicationpool)
        Dim iisObjectPath : iisObjectPath = ("IIS://Localhost/W3SVC/AppPools/" & applicationpool)
        Dim iisObject : Set iisObject = GetObject(iisObjectPath)
                If iisObject.AppPoolState <> 2 Then
                  iisObject.Start
                  If (Err.Number <> 0) Then
                    WScript.Echo "Error starting: " & ObjectPath
                    WScript.Quit (Err.Number)
                  Else
                    WScript.Echo applicationpool & " started."
                  End If
                End If
        Set iisObject = nothing
        Set iisObjectPath = nothing
End Sub

'
' if an application pool is stopped, start + recycle + stop it
'
Sub start_recycle_stop_app(applicationpool)
        Dim iisObjectPath : iisObjectPath = ("IIS://Localhost/W3SVC/AppPools/" & applicationpool)
        Dim iisObject : Set iisObject = GetObject(iisObjectPath)
                If iisObject.AppPoolState <> 2 Then
                  iisObject.Start
                  If (Err.Number <> 0) Then
                    WScript.Echo "Error starting: " & ObjectPath
                    WScript.Quit (Err.Number)
                  Else
                    WScript.Echo applicationpool & " started."
                 End If

                  iisObject.recycle
                  ' we need to sleep for some time because recyle takes some time
                  wscript.sleep(3000)

                  iisObject.Stop
                End If
        Set iisObject = nothing
        Set iisObjectPath = nothing
End Sub

'
' just issue a start command to start an application pool
'
Sub start_given_app(applicationpool)
        Dim iisObjectPath : iisObjectPath = ("IIS://Localhost/W3SVC/AppPools/" & applicationpool)
        Dim iisObject : Set iisObject = GetObject(iisObjectPath)
                IIsObject.Start
                If (Err.Number <> 0) Then
                        WScript.Echo "Error starting: " & ObjectPath
                        WScript.Quit (Err.Number)
                Else
                        WScript.Echo applicationpool & " started."
                End If
        Set iisObject = nothing
        Set iisObjectPath = nothing
End Sub

'
' support function
'
Function State2Desc(nState)
    Select Case nState
    Case 1
        State2Desc = "Starting"
    Case 2
        State2Desc = "Started"
    Case 3
        State2Desc = "Stopping"
    Case 4
        State2Desc = "Stopped"
    Case Else
        State2Desc = "Unknown state"
    End Select
End Function

(部分摘自 http://www.saotn.org/iis-60-start-gestopte-application-pools/ ,这是一个启动所有应用程序池的脚本)。

另存为“startapp.vbs”并运行:

cscript.exe /nologo startapp.vbs name_of_appPool

如果您在不带参数的情况下启动它,则脚本将遍历配置数据库中的所有应用程序池,并在它们未运行时启动它们。

我认为您需要“start_one_app_if_stopped”子目录,因此取消注释该行(第 13 行)并使用命令行参数运行 .vbs 脚本:

cscript.exe /nologo startapp.vbs name_of_appPool

HTH

关于iis - 使用 iisapp.vbs 启动 MyAppPool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14857004/

相关文章:

iis - 完整路径的Kibana可视化

asp.net - 应用程序池和工作进程线程之间是什么关系?

batch-file - 使用 "rmdir"功能但保留指定目录

WCF类型错误和内部服务器错误代码500?

wcf - 在自定义 IIS 帐户下运行的远程调试 WCF 服务

c# - "The RSA key container could not be opened"即使在 ACL 权限之后仍然出错(对于某些用户)

php - Apache 和 IIS 7 一起运行

windows - 重定向脚本内 Windows 批处理文件的输出

windows - 如何通过 Windows 10 中的批处理脚本获取特定适配器的 ipv4 地址

c# - 如何以编程方式配置 IIS 6?