當每天要監控一堆 Server 的 Windows Tasks 就是件累人的事
透過 VB.NET 寫一支自動通報執行結果的程式
把各 Server 的 Windows Tasks 執行結果 Email 出來
應該會省點功...
首先利用 schtasks.exe 將 Windows Tasks 的執行狀況匯出為 csv
所以我寫了一隻 .bat 來做這件事
要注意的是, 我這邊實現的環境是 Windows Server 2003
Win7 的 schtasks.exe 匯出的格式和 Windows Server 2003 不太相同
如果要在 Winy 環境上實現, 可能要再花一些時間分析匯出的 csv
TasksToCSV.bat 的內容 :
schtasks -query -fo csv -v > Tasks.csv
接下來就是 Coding Time 了
就僅針對產生 csv 及擷取內容的方法記錄
Imports System.IO
Imports System.Diagnostics
Imports System.Text
Private Sub GenerateTaskCSV()
' 清除舊的 CSV
Dim fiCSV As New FileInfo(Directory.GetCurrentDirectory() & "\Tasks.csv")
If fiCSV.Exists = True Then
fiCSV.Delete()
End If
' 將 Windows Tasks 匯入新的 CSV
Dim p As Process = Process.Start("TasksToCSV.bat")
' 等待 .bat 執行結束
p.WaitForExit()
End Sub
Private Function GetMailContent(ByVal FilePath As String) As StringBuilder
Dim sbMailContent As New StringBuilder
' 設定要查詢的欄位
Dim aryColumns() As String = {"主機名稱", "工作名稱", "狀態", "上次執行時間"}
Dim intServerNamePos As Integer = 0
Dim intStatusPos As Integer = 2
Dim aryColPos(aryColumns.Length) As Integer
Dim strLine As String = ""
sbMailContent.AppendLine("<table>")
sbMailContent.AppendLine(" <tr>")
For t As Integer = 0 To aryColumns.Length - 1
sbMailContent.AppendLine("<th>" & aryColumns(t) & "</th>")
Next
sbMailContent.AppendLine(" </tr>")
Dim srReader As New StreamReader(FilePath, Encoding.Default)
strLine = srReader.ReadLine()
If Len(strLine) > 0 Then
' 找要查詢欄位的位置
Dim aryHeader() As String = strLine.Split(""",""")
For c As Integer = 0 To aryColumns.Length - 1
For h As Integer = 0 To aryHeader.Length - 1
If aryColumns(c) = Replace(aryHeader(h), """", "") Then
aryColPos(c) = h
h = aryHeader.Length
End If
Next
Next
strLine = srReader.ReadLine()
While (Len(strLine) > 0)
' 如果抓出來的行資料最後不是雙引號結尾, 表示資料跨行
' 就繼續抓出下一行, 直到以雙引號結尾
While (Right(strLine, 1) <> """")
strLine = strLine & srReader.ReadLine()
End While
Dim aryContent() As String = strLine.Split(""",""")
Dim strColumnValue As String = ""
sbMailContent.AppendLine(" <tr>")
For c As Integer = 0 To aryColumns.Length - 1
If Len(ServerName) = 0 AndAlso c = intServerNamePos Then
ServerName = Replace(aryContent(aryColPos(c)), """", "")
End If
strColumnValue = Replace(aryContent(aryColPos(c)), """", "")
sbMailContent.AppendLine("<td>" & strColumnValue & "</td>")
Next
sbMailContent.AppendLine(" </tr>")
strLine = srReader.ReadLine()
End While
End If
sbMailContent.AppendLine("</table>")
Return sbMailContent
End Function
Imports System.Diagnostics
Imports System.Text
Private Sub GenerateTaskCSV()
' 清除舊的 CSV
Dim fiCSV As New FileInfo(Directory.GetCurrentDirectory() & "\Tasks.csv")
If fiCSV.Exists = True Then
fiCSV.Delete()
End If
' 將 Windows Tasks 匯入新的 CSV
Dim p As Process = Process.Start("TasksToCSV.bat")
' 等待 .bat 執行結束
p.WaitForExit()
End Sub
Private Function GetMailContent(ByVal FilePath As String) As StringBuilder
Dim sbMailContent As New StringBuilder
' 設定要查詢的欄位
Dim aryColumns() As String = {"主機名稱", "工作名稱", "狀態", "上次執行時間"}
Dim intServerNamePos As Integer = 0
Dim intStatusPos As Integer = 2
Dim aryColPos(aryColumns.Length) As Integer
Dim strLine As String = ""
sbMailContent.AppendLine("<table>")
sbMailContent.AppendLine(" <tr>")
For t As Integer = 0 To aryColumns.Length - 1
sbMailContent.AppendLine("<th>" & aryColumns(t) & "</th>")
Next
sbMailContent.AppendLine(" </tr>")
Dim srReader As New StreamReader(FilePath, Encoding.Default)
strLine = srReader.ReadLine()
If Len(strLine) > 0 Then
' 找要查詢欄位的位置
Dim aryHeader() As String = strLine.Split(""",""")
For c As Integer = 0 To aryColumns.Length - 1
For h As Integer = 0 To aryHeader.Length - 1
If aryColumns(c) = Replace(aryHeader(h), """", "") Then
aryColPos(c) = h
h = aryHeader.Length
End If
Next
Next
strLine = srReader.ReadLine()
While (Len(strLine) > 0)
' 如果抓出來的行資料最後不是雙引號結尾, 表示資料跨行
' 就繼續抓出下一行, 直到以雙引號結尾
While (Right(strLine, 1) <> """")
strLine = strLine & srReader.ReadLine()
End While
Dim aryContent() As String = strLine.Split(""",""")
Dim strColumnValue As String = ""
sbMailContent.AppendLine(" <tr>")
For c As Integer = 0 To aryColumns.Length - 1
If Len(ServerName) = 0 AndAlso c = intServerNamePos Then
ServerName = Replace(aryContent(aryColPos(c)), """", "")
End If
strColumnValue = Replace(aryContent(aryColPos(c)), """", "")
sbMailContent.AppendLine("<td>" & strColumnValue & "</td>")
Next
sbMailContent.AppendLine(" </tr>")
strLine = srReader.ReadLine()
End While
End If
sbMailContent.AppendLine("</table>")
Return sbMailContent
End Function