Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

send the HTML file to email

Status
Not open for further replies.

Sheetal31

Technical User
Jul 12, 2019
1
SG
hello All,
I have this VBS script to fetch the disk space and it opens as HTML file and please help me to send this via email, please need urgent help




'-----------------------------------------------------------------------------
'Purpose : To perform health check activity - free space on drive
'-----------------------------------------------------------------------------

'**************** Open Text File For Reading ************************

FailedCount=0
SucceededCount=0

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Filelocation = ".\Instances.txt"

If objFSO.FileExists(Filelocation) then
Set objTextFile = objFSO.OpenTextFile(Filelocation, ForReading)
else
MsgBox "File does not exists : " & Filelocation , 16 , "Error"
Set objFSO=nothing
wscript.quit
end if

set WshNetwork = WScript.CreateObject("WScript.Network")
sUsername = WshNetwork.UserName
set WshNetwork = Nothing

'******************* Connecting to each Server in Text File *********************
On Error Resume Next

Do Until objTextFile.AtEndOfStream
If Err.Number <> 0 Then
MsgBox "Error when trying to connect to server <" & strComputer & ">" & vbCRLF & "Error Detail: " & err.description,16,"Error"
Err.Clear
end if

strNextLine = objTextFile.Readline
arrServerList = Split(strNextLine , "vbCrLf")
strComputer = arrServerList(0)
html = html & getJobHistory(strComputer)

Loop
objTextFile.close


TableHeader = "<Table border =""1""><tr><td><b>Servername</b></td><td><b>Drive</b></td><td><b>Total Space(GB)</b></td><td><b>Free Space(GB)</b></td><td><b>" & _
"Used Space(GB)</b></td><td><b>Free %</b></td><td><b>Used %</b></td></B></tr>"

html = TableHeader & html & "</Table><br>"

displayHTML "<font size=6><b>Drive Space Utilization </font></b><br><br>" & html, "Drive Space Utilization"


'**********************************************************************************************************************************************************
'Function
'**********************************************************************************************************************************************************

function getJobHistory(byval ServerName)
set cmd = createobject("ADODB.Command")
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")

cn.open "Provider=SQLOLEDB.1;Data Source=" & ServerName & ";Integrated Security=SSPI"

cmd.activeconnection =cn

cmd.commandtext = "SET NOCOUNT ON DECLARE @hr int DECLARE @fso int DECLARE @drive char(1) DECLARE @odrive int DECLARE @TotalSize varchar(20) DECLARE @FreeSpace varchar(20) DECLARE @MB Numeric " & _
"SET @MB = 1048576 CREATE TABLE #drives (drive char(1) PRIMARY KEY, FreeSpace int NULL, TotalSize int NULL) " & _
" if exists( select 1 from ::fn_servershareddrives()) INSERT #drives(drive) select DriveName from ::fn_servershareddrives() else INSERT #drives(drive,FreeSpace) exec master.dbo.xp_fixeddrives " & _
" EXEC @hr=sp_OACreate 'Scripting.FileSystemObject',@fso OUT IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso " & _
"DECLARE dcur CURSOR LOCAL FAST_FORWARD FOR SELECT drive from #drives ORDER by drive " & _
"OPEN dcur FETCH NEXT FROM dcur INTO @drive WHILE @@FETCH_STATUS=0 " & _
"BEGIN EXEC @hr = sp_OAMethod @fso,'GetDrive', @odrive OUT, @drive IF @hr <> 0 " & _
"EXEC sp_OAGetErrorInfo @fso EXEC @hr = sp_OAGetProperty @odrive,'TotalSize', @TotalSize OUT IF @hr <> 0 " & _
"EXEC sp_OAGetErrorInfo @odrive EXEC @hr = sp_OAGetProperty @odrive,'FreeSpace', @FreeSpace OUT IF @hr <> 0 " & _
"EXEC sp_OAGetErrorInfo @odrive " & _
"UPDATE #drives SET TotalSize=@TotalSize/@MB,FreeSpace=@FreeSpace/@MB WHERE drive=@drive " & _
"FETCH NEXT FROM dcur INTO @drive End Close dcur DEALLOCATE dcur " & _
"EXEC @hr=sp_OADestroy @fso IF @hr <> 0 EXEC sp_OAGetErrorInfo @fso " & _
"SELECT @@servername,drive, TotalSize/1024 as 'Total(GB)', FreeSpace/1024 as 'Free(GB)',(TotalSize/1024)-(FreeSpace/1024) as 'Used(GB)',CAST((FreeSpace/(TotalSize*1.0))*100.0 as int) as 'Free(%)' , 100-CAST((FreeSpace/(TotalSize*1.0))*100.0 as int) as 'Used(%)' FROM #drives ORDER BY drive drop table #drives"
set rs =cmd.execute
'message1="<Table border =""1""><tr><td><b>Servername</b></td><td><b>Drive</b></td><td><b>Total Space(GB)</b></td><td><b>Free Space(GB)</b></td><td><b>" & _
'"Used Space(GB)</b></td><td><b>Free %</b></td><td><b>Used %</b></td></B></tr>"

while rs.eof<>true and rs.bof<>true
FailedFlag = 0

if rs(6) > 85 then
FailedFlag = 1
fontColour="#ff0000"
else
fontColour="#0F00CD"

end if

if FailedFlag = 1 or ShowAll then
message1 = message1 & "<td><font color='" & fontColour & "'>" & rs(0) & "</font></td><td><font color='" & fontColour & "'>" & _
rs(1) & "</font></td><td><font color='" & fontColour & "'>" & rs(2) & "</font></td><td><font color='" & fontColour & "'>" & _
rs(3) & "</font></td><td><font color='" & fontColour & "'>" & rs(4) & "</font></td><td><font color='" & fontColour & "'>" & _
rs(5) & "%</font></td><td><font color='" & fontColour & "'>" & rs(6) &"%</font></td>"

end if

rs.movenext
message1=message1 & "</tr></font>"
wend

getJobHistory = message1
cn.close

end function

sub displayHTML(byval html, Title)

On Error Resume Next

Set objExplorer = CreateObject("InternetExplorer.Application")

objExplorer.Navigate "about:blank"

objExplorer.AddressBar = True
objExplorer.MenuBar = True
objExplorer.StatusBar = True
objExplorer.ToolBar = True
objExplorer.Visible = True

objExplorer.Document.Title = Title
objExplorer.Document.Body.InnerHTML = html



end sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top