' edit **************** 修改區 內邊作修改即可
按月為檔生成
fileFolder 存檔資料夾 後面接 年月\機器名稱.html
altersp 第一段 html 檔案以紅色標籤為記
criticalsp 第二段 以mail通知
其mail部份 請參考
下載 r_FreeHdd_Mail_html.vbs
 
' Script name : r_FreeHdd_Mail_html.vbs
'單機板 or 多機版
' 2006/01/13
' 硬碟剩餘  記錄
'  檢查大小 不夠發mail 單機板
' 2006/06/13 V2
' %0 為此檔案名稱
'  增加參數 /s  單機型  %0 /s 192.168.1.50
'  增加參數 /f 檔案型  %0  /f C:\ServerList.txt
'  增加參數 /? 乎叫說明 %0 /?  
'  不加參數 為本機型
'
' 2006/07/30 v3
' 更換成為按月為檔的資料夾
Option Explicit
dim FSO,objShell,objNet
dim objWMIService,colDisks
dim strComputer
dim strm,strd,stry,strToday,strYM
dim file,myfile,file2,myfile2,fileFolder
dim diskName,disksize,diskfreeS,objDisk
dim hdden
dim mailfrom,mailto,mailsub,mailmess,MXserver,mailsmtpport,mailuser,mailpasswd
dim objEmail,msgerr
dim fush,fuc
Dim strFlag,intState,i
dim add,addfile,userpd
dim strsmptaut,smptaut
dim strsmtpssl,smtpssl
dim strCsize,strDsize
dim ServerList,objfile
dim altersp,criticalsp
call datt
'Set
Set FSO      = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")
set objNet   = CreateObject("WScript.NetWork")
' edit **************** 修改區
' V3 2006/07/30 Edit
'請自行修改以下資訊
' Edit 修改區
fileFolder="D:\mydoc\vbtools\HDD\" & strYM
If not FSO.FolderExists(fileFolder) Then  FSO.CreateFolder(fileFolder)
file  = fileFolder &  "\Frf_"   ' 路徑
file2 = fileFolder &  "\nofr"
hdden = 0
add = 0
userpd = 0
' alter
altersp = 10
' critical
criticalsp = 3
MXserver = "smtp.gmail.com"
mailfrom = "abc@gmail.com"
mailto   = "abcget@gmail.com.tw"
Mailsub =  "硬碟空間不足"
mailmess = "硬碟空間不足" & strComputer & now
mailuser         =  "abc"   '驗證用的帳號
mailpasswd       =  "abcpassword"  '驗證用的密碼
if mailUser <> "" then userpd = userpd + 1
if mailpasswd <> "" then userpd =  userpd + 1
mailsmtpport = 465
strsmptaut = "Yes"  '  Smtp 要驗證
if strsmptaut = "Yes"  then smptaut = 1
strsmtpssl = "Yes"   '  Smtp SSL
if strsmtpssl = "Yes" then SmtpSSL = 1
' ******************  program********************
if Wscript.arguments.Count <> 0 then
if Wscript.arguments.Item(0) = "/?" then call Show_Usage
end if
if Wscript.arguments.Count > 1 then
for i = 0 to Wscript.arguments.Count -1
if Wscript.arguments.Item(i) ="/?" then call Show_Usage
next
for i = 0 to Wscript.arguments.Count -1
select case LCase(Wscript.arguments.Item(i))
case "/f"
ServerList = Wscript.arguments.Item(i+1)
If FSO.FileExists(Wscript.arguments.Item(i+1)) Then
    Set objfile = FSO.OpenTextFile(ServerList,1)
    wscript.echo "檔案型"
    Do While objfile.AtEndOfStream <> True
       strComputer = objfile.ReadLine
       wscript.echo strComputer
       call listdo
    loop
    objfile.Close
    else
    Wscript.Echo "檔案不存在"
End If
case "/s"
strComputer = LCase(Wscript.arguments.Item(i+1))
wscript.echo "單機型"
wscript.echo strComputer
call listdo
end select
next
else
strComputer = objNet.ComputerName
wscript.echo "本機型"
call listdo
End if
' ****************** sub program********************
' 單機or多機
sub listdo
call wmistart
call todaylist    
call mlt
end sub
sub Show_Usage
wscript.echo "Server Free Hdd Space "
wscript.echo "End"
wscript.quit
end sub
sub wmistart
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("Select Name,Size,FreeSpace from Win32_LogicalDisk where DriveType = 3")
end sub
    
sub todaylist
    If not FSO.FileExists(file & strComputer &".html") Then
    set  myfile = FSO.CreateTextFile(file & strComputer &".html",True) 'True:OverWrite
        myfile.writeline  "    <head><title>" & strComputer  & "</title></head>"
        myfile.writeline  "CreatTime" & " " & stry&strm&strd & " " & time  & "</br>"
        myfile.writeline  "Hostname " & "," & strComputer & "</br>"
        myfile.writeline  "<hr>"
        myfile.writeline  "<table border=4>"
        call makefile
    else  
    set  myfile = FSO.OpenTextFile(file & strComputer &".html",8)
    end if  
    myfile.write "<tr><td>" & now & "</td>"
    For each objDisk in colDisks
        diskName  = objDisk.Name
        disksize  = objDisk.Size /1024/1024 /1024
        diskfreeS = objDisk.FreeSpace /1024/1024 /1024
        
        if diskfreeS < altersp then
        myfile.write "<th bgcolor=red>" & diskfrees & "</th>"
        else
        myfile.write "<td>" & diskfrees & "</td>"
        end if
    'if Lcase(diskname) <> "c:" then  
         if diskfreeS < criticalsp then
        hdden = 2
             If not FSO.FileExists(file2 & strComputer &".csv") Then
                      set  myfile2 = FSO.CreateTextFile(file2 & strComputer &".csv") 'True:OverWrite
            else  
                set  myfile2 = FSO.OpenTextFile(file2 & strComputer &".csv",8)
            end if  
                myfile2.writeline     now
                myfile2.writeline    "Partition,ToTalSize,FreeSpace"
                myfile2.write  diskName  &  "," & disksize & "," & diskfreeS
                myfile2.writeline ""
        end if
    'end if
    diskName    =0
    disksize     =0
    diskfreeS     =0
    if hdden = 2 then
     myfile2.writeline ""
     myfile2.close
    end if
    Next
    myfile.writeline ""
    myfile.close
    wscript.sleep 1000
end sub
sub makefile
    myfile.write "<tr><td>" & "Partition" & "</td>"    
    For each objDisk in colDisks
       diskName  = objDisk.Name
       myfile.write   "<td>" & diskname  & "</td>"
    next
    myfile.write  "</tr>"
    myfile.writeline ""
    myfile.write  "<tr><td>" & "Date &     Time" & "</td>"
    For each objDisk in colDisks
        disksize  = objDisk.Size /1024/1024 /1024
        myfile.write  "<td>" & disksize  & "</td>"
    next
    myfile.write  "</tr>"
    myfile.writeline ""
end sub
sub datt
stry = DatePart("yyyy",Date())
strm = DatePart("m",Date())
strd = DatePart("d",Date())
If len(trim(strd)) = 1 Then strd = "0" & strd   
If len(trim(strm)) = 1 Then strm = "0" & strm   
strToday = stry & strm & strd  
'V3 New Add
strYM = stry & strm
end sub    
sub mlt
if hdden = 0 then
exit sub
elseif hdden=2 then
add=1
end if
Set objEmail = CreateObject("CDO.Message")
mailmess = mailmess & strComputer
with objEmail
.From = mailfrom
.To = mailto
.Subject = mailsub
.Textbody = mailmess
'夾檔案
if add = 1 then
.AddAttachment file2 & strComputer &".csv"
end if
if userpd = 2 then
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailuser
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpasswd
end if
if smptaut = 1 then
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
end if
if smtpssl = 1 then
.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
end if
.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MXserver
.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =  mailsmtpport
.Configuration.Fields.Update
.Send
end with
if add = 1 then
FSO.DeleteFile(file2 & strComputer &".csv")
end if
end sub
 
 
沒有留言:
張貼留言