00001: '+
00002: ' write_to_ie_v2.vbs
00003: '
00004: ' This script demoes how to provide text rendition in a window from VBScript.
00005: ' Trick is to create an Internet Explorer document, then populate the document with data and HTML tags.
00006: '
00007: ' Input       : None
00008: ' Output      : Display within an ie page
00009: ' Side effects: None
00010: '
00011: ' v1 Initial version from http://technet.microsoft.com/en-us/library/ee176580.aspx with some customization.
00012: ' v2.0-0 7-nov-2010 Didier.Morandi at gmail.com (http://didier.morandi.free.fr/vbs): Sort before loading display
00013: '-
00014: 
00015: Option Explicit
00016: 
00017: Dim ObjExplorer, objDocument, objWMIService, DataList, ColServices, Service
00018: Dim strBgColor, strTextColor, strComputer, strComputerName, strScriptInfo
00019: Dim intRunning, intStopped
00020: Const Black = "#000000", White="#FFFFFF", Blue="#0000FF", Green="#00FF00", Yellow="#FFFF00", Red="#FF0000"
00021: Const adVarChar = 200
00022: Const MaxCharacters = 255
00023: 
00024: wscript.echo "Opening Windows display"
00025: 
00026: '--- Creating instance of ie ---
00027: Set objExplorer = CreateObject("InternetExplorer.Application")
00028: objExplorer.Navigate "about:blank"
00029: objExplorer.ToolBar   = 0
00030: objExplorer.StatusBar = 0
00031: objExplorer.Width     = 1024
00032: objExplorer.Height    = 600
00033: objExplorer.Left      = 300
00034: objExplorer.Top       = 100
00035: objExplorer.Visible   = 1
00036: 
00037: '--- This loop allows ie to terminate its initialization before creating a document ---
00038: Do While (objExplorer.Busy)
00039: Loop
00040: 
00041: '--- Creating object document ---
00042: Set objDocument = objExplorer.Document
00043: objDocument.Open
00044: 
00045: strBgColor   = White
00046: strTextColor = Blue
00047: strComputerName = GetComputerName()
00048: 
00049: '--- Building ie page ---
00050: objDocument.Writeln "<html><head><title>Service Status</title></head>"
00051: objDocument.Writeln "<body bgcolor='" & strBgColor & "'>"
00052: objDocument.Writeln "<font face=""Verdana"">"
00053: objDocument.Writeln "<table width='100%'>"
00054: objDocument.Writeln "<tr>"
00055: objDocument.Writeln "<td width='30%'><font color=""" & strTextColor & """<b>Services on " & strComputerName & "</b></font></td>"
00056: objDocument.Writeln "<td width='10%'><font color=""" & strTextColor & """<b>State</b></font></td>"
00057: objDocument.Writeln "<td width='60%'><font color=""" & strTextColor & """<b>Description</b></font></td>"
00058: objDocument.Writeln "</tr>"
00059: 
00060: wscript.echo "loading data..."
00061: 
00062: '--- Creating disconnected recordset ---
00063: Set DataList = CreateObject("ADOR.Recordset")
00064: DataList.Fields.Append "ServiceName", adVarChar, MaxCharacters
00065: DataList.Fields.Append "ServiceState", adVarChar, MaxCharacters
00066: DataList.Fields.Append "ServiceDisplayName", adVarChar, MaxCharacters
00067: DataList.Open
00068: 
00069: '--- Fetching data ---
00070: strComputer = "."
00071: Set objWMIService = GetObject("winmgmts:" _
00072:  & "{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
00073: Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service")
00074: 
00075: For Each Service in colServices
00076:   DataList.AddNew
00077:   DataList("ServiceName")        = Service.Name
00078:   DataList("ServiceState")       = Service.State
00079:   DataList("ServiceDisplayName") = Service.DisplayName
00080:   DataList.Update
00081: Next
00082: 
00083: '--- Sorting data ---
00084: wscript.echo "sorting data..."
00085: DataList.Sort = "ServiceName"
00086: 
00087: '--- Loading data in page ---
00088: intRunning = 0
00089: intStopped = 0
00090: DataList.MoveFirst
00091: 
00092: Do Until DataList.EOF
00093:   objDocument.Writeln "<tr>"
00094:   objDocument.Writeln "<td width='30%'><font size =""2"" color=""" & Blue & """>" &_
00095:                       DataList.Fields.Item("ServiceName") &_
00096:                      "</font></td>"
00097: 
00098:   if DataList.Fields.Item("ServiceState") = "Stopped" then
00099:     strTextColor = Red
00100:     intStopped = intStopped + 1
00101:   else
00102:     strTextColor = Blue
00103:     intRunning = intRunning + 1
00104:   end if
00105: 
00106:   objDocument.writeln "<td width='10%'><font size =""2"" color=""" & strTextColor & """>" &_
00107:                       DataList.Fields.Item("ServiceState") &_
00108:                      "</font></td>"
00109:   objDocument.writeln "<td width='60%'><font size =""2"" color=""" & Blue & """>" &_
00110:                       DataList.Fields.Item("ServiceDisplayName") &_
00111:                      "</font></td>"
00112:   objDocument.Writeln "</tr>"
00113:   DataList.MoveNext
00114: Loop
00115: 
00116: '--- Building footer ---
00117: strScriptInfo = GetScriptEngineInfo()
00118: objDocument.Writeln "</table>"
00119: objDocument.Writeln "---"
00120: objDocument.Writeln "<br>"
00121: objDocument.Writeln "<font size =""2"">Running services: " & intRunning & ", Stopped services: " & intStopped & ". Total: " &_
00122:                      (intRunning + intStopped) & " services on " & strComputerName & ".</font>"
00123: objDocument.Writeln "<br>"
00124: objDocument.Writeln "<font size =""2"">Listing provided by " & Ucase(wscript.ScriptName) & " on " & now &_
00125:                     ". " & strScriptInfo & "</font>"
00126: objDocument.Writeln "</font>"
00127: objDocument.Writeln "</body></html>"
00128: objDocument.Write()
00129: objDocument.Close 
00130: wscript.echo "Display done."
00131: wscript.quit
00132: 
00133: '------------------------------------------------------------------------
00134: Function GetComputerName()
00135: 
00136: Dim objWshNetwork
00137: 
00138: Set objWshNetwork = WScript.CreateObject("WScript.Network")
00139: GetComputerName = objWshNetwork.ComputerName
00140: 
00141: End Function
00142: 
00143: '------------------------------------------------------------------------
00144: Function GetScriptEngineInfo
00145: 
00146:   Dim s
00147:   s = ""
00148:   s = ScriptEngine & " Version "
00149:   s = s & ScriptEngineMajorVersion & "."
00150:   s = s & ScriptEngineMinorVersion & "."
00151:   s = s & ScriptEngineBuildVersion
00152:   GetScriptEngineInfo = s
00153: 
00154: End Function
-----
Listing provided by Line_numbers.vbs on 07/11/2010 17:52:40
(c) 2010 Didier.Morandi (at) gmail dot com
http://Didier.Morandi.Free.fr/vbs
