|
論壇說明 |
歡迎您來到『史萊姆論壇』 ^___^ 您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的! 請點擊這裡:『註冊成為我們的一份子!』 |
|
主題工具 | 顯示模式 |
2004-03-10, 06:03 PM | #1 |
榮譽會員
|
腳本,腳本,讓Windows的管理更輕鬆!!
現在將我已經付諸應用的腳本pub出來,
請各位高手批評斧正,, 若斑竹GGMM看得順眼,順便給個1分兩分的, 真慘,眼看就要邁過6分了,一下子又要從頭開始,,哎ing,,,,, 閒話少說,正式開始。。。 1、腳本名稱xlogzip.vbs 'ScriptName: xLogZip.vbs 'Version: 1.0.2 ' 'Created by ANDY on 1/30/2004 'Last Modified by Andy on 2/2/2004 '====================================================== 'Usage: ' cscript xLogBak.vbs SourcePath DestPath ' SourcePath: 源檔案所在路徑 ' DestPath: 壓縮後檔案所在路徑 ' Example: cscript xLogBak.vbs e:\temp D:\temp ' Warning: 需要第三方程序pkzip.exe,該程序放置於 ' 腳本同一目錄,該程序不支持長檔案名稱 '====================================================== 'Script starting..... on error resume next Dim srcPath, dstPath, tempPath, strCMD, strSpace Dim srcName, dstName, LogExtName, ZipExtName, preLogName Dim argCount, iArg Dim tStart, tNow, tRunTime, tFlag1, tFlag2, tSecond Dim sizeStart, SizeEnd Dim strMsgCopy, strMsgZip, strMsgStart, strMsgEnd, strMsgErr, strMsgSum Dim LogEvIDerr, LogEvIDinfo LogExtName = ".log" ZipExtName = ".zip" preLogName = "in" strSpace = " " sizeStart = Int(0) sizeEnd = Int(100) tFlag1 = Int(0) tFlag2 = Int(0) tSecond = Int(1000) LogEvIDerr = 1 LogEvIDinfo = 4 Set objWSH = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") argCount = WScript.Arguments.Count iArg = 0 If argCount <> 2 Then Call ShowUsage End If tStart = Now '獲取需要壓縮的檔案所在路徑 srcPath = WScript.Arguments(0) If Not objFSO.FolderExists(srcPath) Then Call Fail("目錄" & srcPath & "不存在。") End If If Right(srcPath,1) <> "\" Then srcPath = srcPath & "\" End If '獲取壓縮後的目標路徑 dstPath = WScript.Arguments(1) If Not objFSO.FolderExists(dstPath) Then Call Fail("目錄" & dstPath & "不存在。") End If If Right(dstPath,1) <> "\" Then dstPath = dstPath & "\" End If '獲取需要壓縮的檔案名稱及壓縮後的檔案名稱 tNow = DateAdd("d", -1, now) Call GetDateFile(tNow) strMsgStart = strSpace & "腳本開始於" & Right(CStr(Now),8) WScript.Echo strMsgStart 'Call LogToEventLog(strMsgStart, LogEvIDinfo) '判斷源檔案是否存在 If not objFSO.FileExists(srcPath & srcName) Then strMsgZip = strSpace & srcPath & srcName & vbTab & "檔案不存在" WScript.Echo strMsgZip Call LogToEventLog(strMsgStart & vbCrLf & strMsgZip, LogEvIDerr) WScript.Quit(2) Else '使用第三方程序pkzip開始壓縮源檔案 '注意:該程序在DOS下執行,不支持長檔案名稱及目錄 strCMD = "pkzip.exe " & dstName & " " & srcPath & srcName objWSH.run strCMD Do '使用循環檢查壓縮檔案是否新建成功 Set objZipFile = objFSO.GetFile(dstName) sizeStart = Int(objZipFile.size) WScript.sleep tSecond sizeEnd = Int(objZipFile.size) tFlag1 = tFlag1 + 1 If sizeEnd > 100 and sizeEnd = sizeStart Then WScript.sleep 2 * tSecond tFlag2 = tFlag2 + 1 Exit Do End If If DateDiff("s", tStart, Now) > 300 * tSecond Then strMsgErr = "strSpace & PKzip.exe壓縮檔案時發生錯誤。" WScript.Echo strMsgErr Call LogToEventLog(strMsgErr, LogEvIDerr) Exit Do End If WScript.Echo strSpace & "tFlag1:" & tFlag1 & vbTab & "tFlag2:" & tFlag2 & vbTab & sizeStart & vbTab & sizeEnd Loop WScript.Echo strSpace & "tFlag1:" & tFlag1 & vbTab & "tFlag2:" & tFlag2 & vbTab & sizeEnd tRunTime = tFlag1 + 2 * tFlag2 strMsgZip = strSpace & "檔案" & dstName & "新建成功,耗時 " & tRunTime & " 秒" WScript.Echo strMsgZip 'Call LogToEventLog(strMsgZip, LogEvIDinfo) '將壓縮後的檔案copy到目標路徑 objFSO.CopyFile dstName, dstPath, True If objFSO.FileExists(dstPath & dstName) Then strMsgCopy = strSpace & "拷貝" & dstName & "到" & dstPath & "時成功。" WScript.Echo strMsgCopy Call LogToEventLog(strMsgCopy, LogEvIDinfo) Else strMsgCopy = strSpace & "拷貝" & dstName & "到" & dstPath & "時失敗。" WScript.Echo strMsgCopy Call LogToEventLog(strMsgCopy, LogEvIDerr) End If '刪除源日誌檔案。 objFSO.DeleteFile(srcPath & srcName), True '刪除臨時壓縮檔案。 objFSO.DeleteFile(dstName), True '確保上述檔案有足夠的時間被刪除 Wscript.Sleep 10 * tSecond End If strMsgEnd = strSpace & "日誌檔案壓縮結束,耗時" & DateDiff("s", tStart, Now) & "秒。" & vbCrLf & _ strSpace & "腳本結束於" & Right(CStr(Now),8) WScript.Echo strMsgEnd strMsgSum = strMsgStart & vbCrLf & strMsgZip & vbCrLf & strMsgCopy & vbCrLf & strMsgEnd Call LogToEventLog(strMsgSum, LogEvIDinfo) 'Script Ended..... '======================== Function GetDateFile(tNowTime) Dim tYear, tMonth, tDay tYear = DatePart("yyyy",tNowTime) tMonth = DatePart("m",tNowTime) tDay = DatePart("d",tNowTime) if tMonth < 10 then tMonth = "0" & tMonth end if if tDay < 10 then tDay = "0" & tDay end if srcName = preLogName & mid(tYear & tMonth & tDay,3) & LogExtName dstName = preLogName & mid(tYear & tMonth & tDay,3) & ZipExtName End Function '==================== Sub ShowUsage WScript.Echo "Cscript.exe " & WScript.ScriptName & " SourcePath DestPath " & _ vbCrLf & "SourcePath:" & vbTab & " 源檔案路徑" & vbCrLf & "DestPath:" & vbTab & " 檔案備份路徑" WScript.Quit(1) End Sub '==================== Sub Fail(sMessage) WScript.Echo WScript.ScriptName & ": " & vbCrLf & vbTab & sMessage WScript.Quit(2) End Sub '======================== Sub LogToEventLog(sMessage, nValue) objWSH.LogEvent nValue,WScript.ScriptName & ": " & vbCrLf & sMessage End Sub 然後copy到每台需要處理web日誌的服務器上,寫個bat檔案,設置好job, 這樣每天腳本會將日誌copy到備份路徑,並打包壓縮。 如果需要檢視腳本的執行狀況,只要在事件檢視器內找「WSH」的記錄就可以了。 2、腳本名稱xlogExt.vbs 'ScriptName: xLogExt.vbs 'Version: 1.0.2 ' 'Created by ANDY on 1/30/2004 'Last Modified by Andy on 2/2/2004 '====================================================== 'Usage: ' cscript xLogBak.vbs SourcePath DestPath ' SourcePath: 源檔案所在路徑 ' DestPath: 壓縮後檔案所在路徑 ' Example: cscript xLogBak.vbs e:\temp D:\temp ' Warning: 需要第三方程序pkzip.exe,該程序放置於 ' 腳本同一目錄,該程序不支持長檔案名稱 '====================================================== 'Script starting..... on error resume next Dim srcPath, dstPath, tempPath, strCMD, strSpace Dim srcName, dstName, LogExtName, ZipExtName, preLogName Dim argCount, iArg, fDel Dim tStart, tNow, tRunTime, tFlag1, tFlag2, tSecond Dim sizeStart, SizeEnd Dim strMsgCopy, strMsgUnzip, strMsgStart, strMsgEnd, strMsgErr, strMsgSum Dim LogEvIDerr, LogEvIDinfo LogExtName = ".log" ZipExtName = ".zip" preLogName = "in" strSpace = " " sizeStart = Int(0) sizeEnd = Int(100) tFlag1 = Int(0) tFlag2 = Int(0) tSecond = Int(1000) fDel = false LogEvIDerr = 1 LogEvIDinfo = 4 Set objWSH = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") argCount = WScript.Arguments.Count iArg = 0 If argCount <> 2 Then Call ShowUsage End If tStart = Now '獲取需要壓縮的檔案所在路徑 srcPath = WScript.Arguments(0) If Not objFSO.FolderExists(srcPath) Then Call Fail("目錄" & srcPath & "不存在。") End If If Right(srcPath,1) <> "\" Then srcPath = srcPath & "\" End If '獲取壓縮後的目標路徑 dstPath = WScript.Arguments(1) If Not objFSO.FolderExists(dstPath) Then Call Fail("目錄" & dstPath & "不存在。") End If If Right(dstPath,1) <> "\" Then dstPath = dstPath & "\" End If '獲取需要壓縮的檔案名稱及壓縮後的檔案名稱 tNow = DateAdd("d", -1, now) Call GetDateFile(tNow) strMsgStart = strSpace & "腳本開始於" & Right(CStr(Now),8) WScript.Echo strMsgStart '判斷源檔案是否存在,並將其copy到本地臨時路徑下 If not objFSO.FileExists(srcPath & srcName) Then strMsgCopy = strSpace & srcPath & srcName & vbTab & "檔案不存在" WScript.Echo strMsgCopy Call LogToEventLog(strMsgStart & vbCrLf & strMsgCopy, LogEvIDerr) WScript.Quit(2) Else '將源檔案copy到本地 objFSO.CopyFile srcPath & srcName, ".\", True strMsgCopy = strSpace & "拷貝" & srcName & "到本地成功。" WScript.Echo strMsgCopy '使用第三方程序pkunzip解壓縮檔案到目標路徑 '注意:該程序在DOS下執行,不支持長檔案名稱及目錄, '並且zip檔案只能放置於當前目錄下 strCMD = "pkunzip.exe " & srcName & " " & dstPath objWSH.run strCMD '使用無限循環檢查檔案是否解壓縮成功 WScript.Sleep 15 * tSecond Do Set objLogFile = objFSO.GetFile(dstPath & dstName) If Err.Number <> 0 Then strMsgUnzip = " 錯誤代碼:" & Err.number & vbCrLf & " 錯誤來源:" & Err.Source & vbCrLf & " 錯誤描述:" & Err.Description Err.clear Exit Do End If sizeStart = objLogFile.size WScript.sleep tSecond tFlag1 = tFlag1 + 1 sizeEnd = objLogFile.size If sizeEnd > 100 and sizeEnd = sizeStart Then WScript.sleep 2 * tSecond tFlag2 = tFlag2 + 1 fDel = True Exit Do End If ' If DateDiff("s", tStart, Now) > 100 * tSecond and sizestart = 0 Then ' strMsgErr = strSpace & "PKunzip.exe解壓檔案時發生錯誤或檔案名稱前後不一致。" ' WScript.Echo strMsgErr ' Call LogToEventLog(strMsgErr, LogEvIDerr) ' Exit Do ' End If WScript.Echo strSpace & "tFlag1:" & tFlag1 & vbTab & "tFlag2:" & tFlag2 & vbTab & sizeStart & vbTab & sizeEnd Loop WScript.Echo strSpace & "tFlag1:" & tFlag1 & vbTab & "tFlag2:" & tFlag2 & vbTab & sizeEnd If fDel = True Then tRunTime = tFlag1 + 2 * tFlag2 + 15 strMsgUnzip = strSpace & "解壓" & srcName & "成功,耗時 " & tRunTime & " 秒" WScript.Echo strMsgUnzip '刪除臨時路徑下的壓縮檔案 objFSO.DeleteFile(srcName), True '刪除源路徑下面的壓縮檔案 objFSO.DeleteFile(srcPath & srcName), True '確保上述檔案已經被刪除 Wscript.Sleep 10 * tSecond strMsgEnd = strSpace & "日誌檔案解壓結束,耗時" & DateDiff("s", tStart, Now) & "秒。" & vbCrLf & _ strSpace & "腳本結束於" & Right(CStr(Now),8) WScript.Echo strMsgEnd strMsgSum = strMsgStart & vbCrLf & strMsgCopy & vbCrLf &strMsgUnzip & vbCrLf & strMsgEnd Call LogToEventLog(strMsgSum, LogEvIDinfo) Else strMsgUnzip = strSpace & "解壓" & srcName & "發生意外錯誤" & vbCrLf & strMsgUnzip WScript.Echo strMsgUnzip Call LogToLogEvent(strMsgUnzip, LogEvIDerr) End If End If 'Script Ended..... '======================== Function GetDateFile(tNowTime) Dim tYear, tMonth, tDay tYear = DatePart("yyyy",tNowTime) tMonth = DatePart("m",tNowTime) tDay = DatePart("d",tNowTime) if tMonth < 10 then tMonth = "0" & tMonth end if if tDay < 10 then tDay = "0" & tDay end if srcName = preLogName & mid(tYear & tMonth & tDay,3) & ZipExtName dstName = preLogName & mid(tYear & tMonth & tDay,3) & LogExtName End Function '==================== Sub ShowUsage WScript.Echo "Cscript.exe " & WScript.ScriptName & " SourcePath DestPath " & _ vbCrLf & "SourcePath:" & vbTab & " 源檔案路徑" & vbCrLf & "DestPath:" & vbTab & " 檔案備份路徑" WScript.Quit(1) End Sub '==================== Sub Fail(sMessage) WScript.Echo WScript.ScriptName & ": " & vbCrLf & vbTab & sMessage WScript.Quit(2) End Sub '======================== Sub LogToEventLog(sMessage, nValue) objWSH.LogEvent nValue,WScript.ScriptName & ": " & vbCrLf & sMessage End Sub 然後copy到日誌分析處理的服務器上,寫個bat檔案,設置好job, 這樣每天腳本會將日誌從遠端服務器的備份路徑上copy到本地,並解壓,以供日誌分析程序分析。 如果需要檢視腳本的執行狀況,只要在事件檢視器內找「WSH」的記錄就可以了。 上面兩個腳本是相輔相成的, 本來打算將兩個腳本合併成一個腳本,無奈水平有限,只好分成兩個來寫。 3、腳本DelOldFiles.vbs 'ScriptName: DelOldFiles.vbs 'Version: 1.0.0 ' 'Created by ANDY on 2/10/2004 'Last Modified by Andy on 2/10/2004 '====================================================== 'Usage: ' cscript DelOldFiles.vbs DestPath MaxKeepDays ' DestPath: 待刪除檔案所在路徑 ' MaxKeepDays:檔案保留天數 ' Example: cscript DelOldFiles.vbs e:\temp 10 '====================================================== 'Script starting..... on error resume Next Dim objWSH, objFSO, objFolder, objFiles, argCount, iArg Dim dstPath, MaxKeepDays Dim tDay, tStart, tRun Dim File, strSpace, delFile, delNum Dim strMsgStart, strMsgEnd, strMsgDel, strMsgDels, strMsgErr Dim LogEvIDerr, LogEvIDinfo strSpace = " " LogEvIDerr = 1 LogEvIDinfo = 4 delNum = 0 Set objWSH = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") argCount = WScript.Arguments.Count iArg = 0 If argCount <> 2 Then Call ShowUsage End If '獲取待刪除檔案所在路徑 dstPath = WScript.Arguments(0) If Not objFSO.FolderExists(dstPath) Then Call Fail("目錄" & dstPath & "不存在。") End If If Right(dstPath,1) <> "\" Then dstPath = dstPath & "\" End If '獲取檔案的保留天數 MaxKeepDays = WScript.Arguments(1) If IsNumeric(MaxKeepDays) Then If (MaxKeepDays > 0) Then MaxKeepDays = Int(MaxKeepDays) Else Call Fail("所輸入的MaxKeepDays必須大於0!") End If Else Call Fail("所輸入的MaxKeepDays必須為數位!") End If '開始取得每個檔案的內容 Set objFolder = objFSO.GetFolder(dstPath) Set objFiles = objFolder.Files strMsgStart = strSpace & "開始刪除目錄" & dstPath & "下超過" & MaxKeepDays & "天的檔案!" WScript.Echo strMsgStart tStart = Now For Each File In objFiles delFile = File.Name tDay = Int(DateDiff("d", File.DateLastModified, Now)) If tDay > MaxKeepDays Then objFSO.DeleteFile(File), True If Err.Number <> 0 Then strMsgErr = delFile & vbTab & "檔案刪除失敗!" & vbCrLf &" 錯誤代碼:" & _ Err.number & vbCrLf & " 錯誤來源:" & Err.Source & vbCrLf & _ " 錯誤描述:" & Err.Description Err.clear WScript.Echo strMsgErr Call LogToEventLog(strMsgErr, LogEvIDerr) Else delNum = delNum + 1 strMsgDel = strSpace & delFile & " " & vbTab & "已經存在" & tDay & "天!---刪除!" strMsgDels = strMsgDel & vbCrLf & strMsgDels WScript.Echo strMsgDel End If End If Next tRun = DateDiff("s", tStart, Now) strMsgEnd = strSpace & "完畢,共刪除" & delNum & "個檔案,耗時" & tRun & "秒。" WScript.Echo strMsgEnd Call LogToEventLog(strMsgStart & vbCrLf & strMsgDels & strMsgEnd, LogEvIDinfo) '----End Script----- '==================== Sub ShowUsage WScript.Echo "Parameter Error!!" & vbCrLf & "Cscript.exe " & WScript.ScriptName & _ " DestPath MaxKeepDays " & vbCrLf & "DstPath:" & vbTab & " 待刪除檔案路徑" & _ vbCrLf & "MaxKeepDays:" & vbTab & " 檔案保留天數" WScript.Quit(1) End Sub '==================== Sub Fail(sMessage) If bEventLog Then LogToEventLog sMessage, EVENT_ERROR If IsObject(objLogFile) Then objLogFile.WriteLine(WScript.ScriptName & ": " & vbCrLf & vbTab & sMessage) objLogFile.Close End If WScript.Echo WScript.ScriptName & ": " & vbCrLf & vbTab & sMessage WScript.Quit(1) End Sub '======================== Sub LogToEventLog(sMessage, nValue) objWSH.LogEvent nValue,WScript.ScriptName & ": " & vbCrLf & sMessage End Sub 然後copy到相應服務器上,寫個bat檔案,設置好job, 這樣腳本就會定期刪除超過N天的檔案 如果需要檢視腳本的執行狀況,只要在事件檢視器內找「WSH」的記錄就可以了。 4、MonitorDisk.vbs 'ScriptName: MonitorDisk.vbs 'Version: 1.0.0 ' 'Created by ANDY on 2/22/2004 'Last Modified by Andy on 2/22/2004 '====================================================== 'Usage: ' cscript MonitorDisk.vbs [SrvName0] [SrvName1] [,,,] ' SrvName0: 服務器名稱 ' SrvName1: 服務器名稱 ' Example: cscript MonitorDisk.vbs ' cscript MonitorDisk.vbs jkbdc ' Warning: 需要有相關權限,譬如domain admin可以掃瞄 ' 域內的電腦。如果沒有參數,預設值掃瞄本機。 '====================================================== ' 'Script starting..... Option Explicit On Error Resume Next Dim FreeMegaBytes, SrvName Dim objFSO, objLogFile, objFile, objWMIService, objLogicalDisk, colLogicalDisk Dim NameLength, SpaceToAdd, SpaceHead, MinDisk Dim argCount, iArg, i Dim strMsgErr, strMsgStart, strMsgEnd, strMsgRun Dim fileAttached, tNow Call GetDateFile(Now) Dim aAttachment, sTo, sFrom, sSender, sSubject, sBody If DatePart("w", Now) = 2 Then sTo = "user1@urmail.com;user2@urmail.com;user3@urmail.com" Else sTo = "user1@urmail.com" End If sFrom = "Analysis@urmail.com" 'sSender = sSubject = "服務器磁碟剩餘空間掃瞄" & now sBody = "hi,all" & vbCrLf & "附有服務器磁碟剩餘空間掃瞄結果," & vbCrLf & _ "如果磁碟剩餘空間小於300M,則報告磁碟空間不足,請盡快清理!!" argCount = WScript.Arguments.Count iArg = 0 MinDisk = 300 Const conMegaToBits = 1048576 SpaceHead = Space(2) Set objFSO = CreateObject("Scripting.FileSystemObject") Set objLogFile = objFSO.CreateTextFile(fileAttached, True) If argCount = 0 Then SrvName = "." Call ScanAvailableDisk(SrvName) End If If argCount = 1 and WScript.Arguments(0) = "/?" Then Call ShowUsage Else For i = 0 to argCount - 1 SrvName = WScript.Arguments(i) Call ScanAvailableDisk(SrvName) Next End If objLogFile.Close Wscript.Sleep 1000 Set objFile = objFSO.GetFile(fileAttached) aAttachment = objFile.Path SendAlert sFrom, sTo, sSender, sSubject, sBody, aAttachment '====================== Sub ScanAvailableDisk(SrvName) Set objWMIService = GetObject("winmgmts://" & SrvName) Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk") strMsgStart = "開始掃瞄" & SrvName & "磁碟剩餘空間..." WScript.Echo strMsgStart Call LogToFile(strMsgStart) If colLogicalDisk.Count = 0 Then strMsgRun = "No Drives is installed on " & SrvName Wscipt.Echo strMsgRun Call LogToFile(strMsgRun) Else For Each objLogicalDisk in colLogicalDisk If IsNull(objLogicalDisk.FreeSpace) Then strMsgRun = SpaceHead & Left(objLogicalDisk.DeviceID,1) & "盤:" & Space(12) & "No Media!" WScript.Echo strMsgRun Call LogToFile(strMsgRun) objLogicalDisk.FreeSpace = 0 Else FreeMegaBytes = FormatNumber(objLogicalDisk.FreeSpace / conMegaToBits, 3, true, false, false) Namelength = Len(FreeMegaBytes) SpaceToAdd = 18 - NameLength If FreeMegaBytes < 300 Then strMsgRun = SpaceHead & Left(objLogicalDisk.DeviceID,1) & "盤:" & Space(SpaceToAdd) & FreeMegaBytes & " M!" & "————空間不足,盡快清理!" WScript.Echo strMsgRun Call LogToFile(strMsgRun) Else strMsgRun = SpaceHead & Left(objLogicalDisk.DeviceID,1) & "盤:" & Space(SpaceToAdd) & FreeMegaBytes & " M!" WScript.Echo strMsgRun Call LogToFile(strMsgRun) End If End If Next End If strMsgEnd = SpaceHead & SrvName & "掃瞄結束!" WScript.Echo strMsgEnd Call LogToFile(strMsgEnd) End Sub '==================== Sub ShowUsage WScript.Echo "Usage:" & vbCrLf & SpaceHead & "Cscript.exe " & WScript.ScriptName & " [ServerName0] [ServerName1] [...]" & _ vbCrLf & SpaceHead & "ServerNameX:" & vbTab & " 服務器名稱,如果為空,則掃瞄本機空間。" WScript.Quit(1) End Sub '======================== Function GetDateFile(tNowTime) Dim tYear, tMonth, tDay, tHour tYear = DatePart("yyyy",tNowTime) tMonth = DatePart("m",tNowTime) tDay = DatePart("d",tNowTime) tHour = DatePart("h",tNowTime) If tMonth < 10 Then tMonth = "0" & tMonth End If If tDay < 10 Then tDay = "0" & tDay End If If tHour < 10 Then tHour = "0" & tHour End If fileAttached = tYear & tMonth & tDay & tHour & ".txt" End Function '==================== Sub LogToFile(sMessage) If IsObject(objLogFile) Then objLogFile.WriteLine(sMessage) End Sub '==================== Sub SendAlert(Byval sFrom, Byval sTo, Byval sSender, Byval sSubject, Byval sBody, Byval aAttachment) Dim sAttachment Dim oMsg, oConf, cFlds Const cdoSendUsingMethod = _ "http://schemas.microsoft.com/cdo/configuration/sendusing" Const cdoSMTPServer = _ "http://schemas.microsoft.com/cdo/configuration/SMTPServer" Const cdoSMTPServerPort = _ "http://schemas.microsoft.com/cdo/configuration/SMTPServerPort" Const cdoSMTPAuthenticate = _ "http://schemas.microsoft.com/cdo/configuration/SMTPAuthenticate" Const cdoSendUsingPort = 2 Const cdoAnonymous = 0 Set oMsg = CreateObject("CDO.Message") Set oConf = CreateObject("CDO.Configuration") Set cFlds = oConf.Fields With cFlds .Item(cdoSendUsingMethod) = cdoSendUsingPort .Item(cdoSMTPServer) = "urmail.com" '//Mail server goes here .Item(cdoSMTPAuthenticate) = cdoAnonymous .Update End With With oMsg Set .Configuration = oConf .To = sTo .From = sFrom .Sender = sSender .Subject = sSubject .TextBody = sBody .AddAttachment aAttachment .Send End With Set oMsg = Nothing Set oConf = Nothing End Sub 使用bat設置好job後, 該腳本就會掃瞄bat檔案內所定義的每台電腦的每個邏輯磁碟代號的磁碟空間, 並將掃瞄結果發到u指定的信箱裡去, 這樣,u只要檢視郵件就可以了。。。。。。 如果u指定的信箱發送郵件時需要身份認證,則上述腳本稍有改動。 後記: 上述腳本執行時間將近兩個月,,反應良好。。 當然,也發現有些不盡人意的地方,不過沒什麼大問題,也就懶得動了。。 除了監控磁碟外,CPU、行程、域內所安裝的軟體等等等等都可以,, 只是有些win2000不支持罷了。 上述腳本都只使用到了windows腳本技術的一小部分,主要是用了WSH,, windows腳本還支持多種其他的技術,本人水平有限,在此不便提。 有興趣的可去windows網站參觀, 令大家開心的是,最近微軟改版,已經有很多中文的說明了。。。。 2000SP4和2003之後將內置.net framework 原來以為只是個執行時支持環境 後來發現竟然是完整的命令行開發環境 直接支持vb.net, jsript.net, c#的編譯執行 今後要學好windows script恐怕還是要好好學下jscript.net 改進太大了。 不好意思, 現在才弄上來,, 雖然說是學習筆記,但供我本人參考還勉勉強強, 但供大家參考可能就有點勉為其難了,請包涵,, 因為本人E文有限,有些不知道怎麼翻譯的、或者比較容易明白的, 就直接使用E文了,, 上述的學習摘要只涵蓋了《Windows 2000 Scripting Guide》一書的WSH部分, 內容也許只佔1/10吧,也許對初步接觸稍微有點幫助。 有用的話就鼓鼓掌,沒用的話就扔到垃圾箱裡去吧!! |
送花文章: 3,
|