2013-02-13 46 views
2

我有一個批處理文件,它使用以下腳本來回收MyAppPool使用iisapp.vbs啓動MyAppPool

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

但是,當MyAppPool已停止,那麼我無法回收它。我想要檢查天氣MyAppPool已停止,如果停止,然後啓動它,回收它,然後再次停止。

那麼我是一個完整的新手在這個IIS的東西,從來沒有在它的工作。我正在使用Window Server 2003和IIS6。

回答

3

您可以編寫自己的.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」 子,所以uncoment該線路(13號線),並用命令行參數運行.vbs腳本:

cscript.exe /nologo startapp.vbs name_of_appPool 

HTH

+0

偉大的答案。 ... + 1。在啓動'MyAppPool'之前,我可以檢查'IIsObject'的狀態,以便我可以相應地啓動和停止嗎?這個答案很好,但我不知道vb腳本修改它...... – Sandy 2013-02-18 06:43:49

+0

我試着檢查它的狀態......但找不到方法 – Sandy 2013-02-18 07:18:00

+0

你可以使用iisObject的.AppPoolState屬性(上面的代碼示例)讀取池的狀態。類似於:dim appStatus:appStatus = iisObject.AppPoolState。 – 2013-02-18 09:01:40