2014-01-08 59 views
1

我正在編寫(批處理文件或VBScript)以很好地關閉Windows服務器上所有正在運行的WebSphere JVM,但需要某些文本處理方面的幫助。我希望腳本運行並解析「serverstatus」命令的輸出以獲得名稱應用程序服務器,並將匹配(包含回車符)存儲在變量中以供腳本的其餘部分使用。用於從命令輸出中解析WebSphere JVM的名稱的Windows腳本

示例命令輸出:

C:\WebSphere\AppServer\bin>serverstatus -all 
ADMU0116I: Tool information is being logged in file 
     C:\WebSphere\AppServer\profiles\MySrv01\logs\serverStatus.log 
ADMU0128I: Starting tool with the MySrv01 profile 
ADMU0503I: Retrieving server status for all servers 
ADMU0505I: Servers found in configuration: 
ADMU0506I: Server name: MyCluster_MySrv01 
ADMU0506I: Server name: MyCluster_MySrv01_1 
ADMU0506I: Server name: MyNextCluster_MySrv04 
ADMU0506I: Server name: MyNextCluster_MySrv04_1 
ADMU0506I: Server name: nodeagent 
ADMU0508I: The Application Server "MyCluster_MySrv01" is STARTED 
ADMU0508I: The Application Server "MyCluster_MySrv01_1" is STARTED 
ADMU0508I: The Application Server "MyNextCluster_MySrv04" is STARTED 
ADMU0509I: The Application Server "MyNextCluster_MySrv04_1" cannot be 
     reached. It appears to be stopped. 
ADMU0508I: The Node Agent "nodeagent" is STARTED 

*節點代理應不匹配。評委仍然不知道我是要瞄準所有的應用服務器,還是僅僅是那些狀態爲「STARTED」的應用服務器。

回答

1

下面是使用正則表達式的替代方案。它只是讀取stdout並處理所有已啓動的應用程序服務器 - 應用程序服務器存儲在名爲AppServers的數組中。測試W2K3。 編輯:我們添加了一種方法,通過添加日誌寫入功能將輸出記錄到文件中(不要忘記在腳本的開頭添加const ForAppending,我們剛剛添加到此答案中)。日誌寫入功能採用以下格式:

Logwrite "some text to write - delete file if exists", "c:\Path\filename.txt", 1 
Logwrite "some text to write - append to file, don't delete", "c:\path\filename.txt", 0 

這是一個粗略的功能,但按照你的要求。我希望有所幫助。 :)

option explicit 
Const ForAppending = 8 
Dim objShell, objWshScriptExec, objStdOut 
Dim objCmdString, strLine, appServers(), maxAppServers 
Dim x 

' File Path/Location to serverstatus.bat ---- 
objCmdString = "C:\WebSphere\AppServer\bin\serverstatus.bat -all" 

Set objShell = CreateObject("WScript.Shell") 
Set objWshScriptExec = objShell.Exec(objCmdString) 
Set objStdOut = objWshScriptExec.StdOut 

MaxAppServers = -1 

' While we're looping through the response from the serverstatus command, look for started application servers 
' and store them in an ever expanding array AppServers. 
' The Variable MaxAppServers should always contain the highest number of AppServers (ie: ubound(AppServers)) 

While Not objStdOut.AtEndOfStream 
    strLine = objStdOut.ReadLine 
    If InStr(LCase(strLine), "admu0508i: the application server """) Then 
     MaxAppServers = MaxAppServers + 1 
     ReDim Preserve AppServers(MaxAppServers) 
     AppServers(MaxAppServers) = wedge(strLine, Chr(34)) 
    End If 
Wend 

If MaxAppServers => 0 then 
    For x = 0 To ubound(AppServers) ' You could just use For x = 1 to MaxAppServers in this case. 
     ' Add your instructions here......... 
     ' ... We are simply echoing out the AppServer name below as an example to a log file as requested below. 
     Logwrite AppServers(x), "c:\Output.log", 0 
    Next 
End If 

Function Wedge(wStr, wOpr) 
' This clunky function simply grabs a section of a string the is encapsulated by wOpr. 
' NOTE: This function expects wOpr to be a single character (eg; for our purpose, it is pulling data between double quotes). 

    Dim wFlag, wCount, wFinish 

    wflag = False 
    wFinish = False 
    wCount = 1 
    Wedge = "" 

    Do Until wCount > Len(wStr) Or wFinish 
     If Mid(wStr, wCount, 1) = wOpr Then 
      If wFlag Then 
       wFinish = True 
      Else 
       wFlag = True 
      End If 
     Else 
      If wFlag Then Wedge = Wedge & Mid(wStr, wCount, 1)         
     End If 
     wCount = wCount + 1 
    Loop 
End Function 

Function logwrite (lstrtxt, lwLogfile, lwflag) 
    Dim lwObjFSO, lwObjFile, fstr, lwcounter, lwc 
    fstr = lstrtxt 
    Set lwObjFSO = CreateObject("Scripting.FileSystemObject") 
    If lwflag=1 And lwObjFSO.FileExists(lwLogFile) Then lwObjfso.deletefile(lwLogFile) 
    If lwObjFSO.FileExists(lwLogFile) then 
     On Error Resume next 
     Set lwObjFile = lwObjFSO.OpenTextFile(lwLOgFile, ForAppending) 
     lwCounter = 20000 
     Do While Err.number = 70 And lwCounter > 0 
      wscript.echo "ERROR: Retrying output - Permission denied; File may be in use!" 
      For lwc = 1 To 1000000 
      Next 
      Err.clear 
      Set lwObjFile = lwObjFSO.OpenTextFile(lwLogFile, ForAppending) 
      lwCounter = lwCounter-1 
     Loop 
     If Err.number <> 0 Then 
      wscript.echo "Error Number: "&Err.number 
      wscript.quit 
     End If 
     On Error goto 0 
    Else 
     Set lwObjFile = lwObjFSO.CreateTextFile(lwLogFile) 
    End If 
    wscript.echo (fstr) 
    lwObjFile.Write (fstr) & vbcrlf 
    lwObjFile.Close 
    Set lwObjFSO=Nothing 
    Set lwObjfile=Nothing 

End Function 
+0

這個腳本是一個巨大的幫助,有什麼需要我做起來。在沒有在每個盒子上安裝第三方TEE實用程序的情況下,還可以在日誌文件中獲得輸出的最佳方式是什麼? – user2655065

+0

我在上面的代碼示例中添加了一個Logwrite函數。我希望有所幫助。 – Damien

1

使用RegExp從輸入中剪切引用的名稱;添加上下文 - 服務器,開始 - 微調結果集。在代碼:

Option Explicit 

Function q(s) : q = "'" & s & "'" : End Function 

Dim sInp : sInp = Join(Array(_ 
    "ADMU0116I: Tool information is being logged in file C:\WebSphere\AppServer\profiles\MySrv01\logs\serverStatus.log" _ 
, "ADMU0128I: Starting tool with the MySrv01 profile" _ 
, "ADMU0503I: Retrieving server status for all servers" _ 
, "ADMU0505I: Servers found in configuration:" _ 
, "ADMU0506I: Server name: MyCluster_MySrv01" _ 
, "ADMU0506I: Server name: MyCluster_MySrv01_1" _ 
, "ADMU0506I: Server name: MyNextCluster_MySrv04" _ 
, "ADMU0506I: Server name: MyNextCluster_MySrv04_1" _ 
, "ADMU0506I: Server name: nodeagent" _ 
, "ADMU0508I: The Application Server ""MyCluster_MySrv01"" is STARTED" _ 
, "ADMU0508I: The Application Server ""MyCluster_MySrv01_1"" is STARTED" _ 
, "ADMU0508I: The Application Server ""MyNextCluster_MySrv04"" is STARTED" _ 
, "ADMU0509I: The Application Server ""MyNextCluster_MySrv04_1"" cannot be reached. It appears to be stopped." _ 
, "ADMU0508I: The Node Agent ""nodeagent"" is STARTED" _ 
), vbCrLf) 

Dim aRes : aRes = Array(_ 
    Array("all quoted names", """([^""]+)""") _ 
    , Array("all quoted started servers", "Server ""([^""]+)"" is STARTED") _ 
) 

Dim aRE 
For Each aRe In aRes 
    WScript.Echo "----------------", q(aRe(0)), q(aRe(1)) 
    Dim re : Set re = New RegExp 
    re.Global = True 
    re.Pattern = aRe(1) 
    Dim oMTS : Set oMTS = re.Execute(sInp) 
    ReDim a(oMTS.Count - 1) 
    Dim i 
    For i = 0 To UBound(a) 
     a(i) = q(oMTS(i).SubMatches(0)) 
    Next 
    WScript.Echo " =>", Join(a) 

Next 

輸出:

cscript 20984738.vbs 
---------------- 'all quoted names' '"([^"]+)"' 
=> 'MyCluster_MySrv01' 'MyCluster_MySrv01_1' 'MyNextCluster_MySrv04' 'MyNextCluster_MySrv04_1' 'nodeagent' 
---------------- 'all quoted started servers' 'Server "([^"]+)" is STARTED' 
=> 'MyCluster_MySrv01' 'MyCluster_MySrv01_1' 'MyNextCluster_MySrv04'