Pages

samedi 26 novembre 2011

wevtutil et Vbscript


Comme je vous l'avais indiqué précédemment, il me semblait interessant d'essayer d'exploiter les informations obtenues par Wevtutil en Vbscript.

Toujours dans l'idée de récupérer les informations indiquées sur le site de techrepublic, j'ai réalisé un petit script.

Attention, ce script  s’exécutera seulement avec des privilèges élevés.



Voici donc un exemple de la manière de réaliser cette tâche :

' ---------------------------------------------------------------------------
'                        Variable creation
' ---------------------------------------------------------------------------
StrComputer = "."
CsvFl = "InstallBoot-1-0.csv"
StrPATH = "D:\Projets\Fred\temp\bootime"

Set Fso = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("Wscript.Shell")
Set WshShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & StrComputer & "\root\cimv2")

Dim xmlDoc, xmlHeader, Btime, MPBtime, PBtime, BStime, MAC, MacAddr
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlHeader ="<?xml version='1.0' encoding='iso-8859-1'?>" ' XML Header

Set tmpFolder = Fso.GetSpecialFolder(2) ' Define Temporary Folder

' ---------------------------------------------------------------------------
'                                 BootTime
' ---------------------------------------------------------------------------
' Clean TmpOutput XML File
If Fso.FileExists(tmpFolder & "\TmpbootTime.xml") Then
 Set OutputXML  = Fso.GetFile(tmpFolder & "\TmpbootTime.xml")
 OutputXML.Delete
End If

' Clean Output XML File
If Fso.FileExists(tmpFolder & "\bootTime.xml") Then
 Set OutputXML  = Fso.GetFile(tmpFolder & "\bootTime.xml")
 OutputXML.Delete
End If

' Request Events Log, create tmp XML file

QueryWevtutil = "cmd /c %windir%\system32\wevtutil.exe qe Microsoft-Windows-Diagnostics-Performance/Operational /rd:true /f:xml /c:1 /q:" & chr(34) & "*[System[(Level=1  or Level=2 or Level=3 or Level=4 or Level=0 or Level=5) and (EventID = 100)]]" & chr(34) &" > " & tmpFolder & "\TmpbootTime.xml"

'msgbox QueryWevtutil
WshShell.run(QueryWevtutil)
wscript.sleep 2*1000 ' Pause 2s

' Read the content of the temporary File
Set TmpOutputXML = Fso.OpenTextFile(tmpFolder & "\TmpbootTime.xml", 1, True)
file = Split(TmpOutputXML.ReadAll(), vbCrLf)
TmpOutputXML.Close()
Set TmpOutputXML = Nothing

' open the final file, Add the XML Header and the content of the tmp file
Set OutputXML = Fso.OpenTextFile(tmpFolder & "\bootTime.xml", 8, True)
OutputXML.WriteLine(xmlHeader)
for i = LBound(file) to UBound(file)
     OutputXML.WriteLine(file(i))
Next
OutputXML.Close()
Set OutputXML = Nothing

wscript.sleep 1*1000 ' Pause 1s

' Read the content of the previously created XML file
xmlDoc.Async = "false"
xmlDoc.Load(tmpFolder & "\bootTime.xml") ' XML File Loaded

For Each EventElement In xmlDoc.selectNodes("Event") 'Enter into the <Event> Node
 Set EventData = EventElement.selectNodes("EventData") 'Enter into the <EventData> Node
 For Each EventDataElement In EventData
  Set DataNode = EventDataElement.selectNodes("Data")
  For Each SubElement In DataNode
   DataNodeAttr = SubElement.getAttribute("Name")
   Select Case SubElement.getAttribute("Name")
    Case "BootTime"
     Btime = ""
     Btime = SubElement.text / 1000
    Case "MainPathBootTime"
     MPBtime = ""
     MPBtime = SubElement.text / 1000
    Case "BootPostBootTime"
     PBtime = ""
     PBtime = SubElement.text / 1000
    Case "BootStartTime"
     BStime = ""
     BStime = left(SubElement.text, 16)
     BStime = Replace(BSTime,"T"," ")
    Case Else
     Output = "Not Found;Not Found;Not Found;"
   End Select
  Next

' Debug Mode
' MsgBox "Boot Time : " & Btime & vbcrlf & _
'   "Main Path Boot Time: " & MPBtime & vbcrlf & _
'   "Boot Post Boot Time  : " & PBtime
 Next 'End Second loop

Next 'End first loop

Set xmlDoc = Nothing

' ---------------------------------------------------------------------------
'                    Search for Computer Type : Computer
' ---------------------------------------------------------------------------
'On Error Resume Next

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct",,48)
For Each objItem in colItems
Vendor = objItem.Vendor
Next

If InStr(LCase(Vendor), LCase("VMware")) Then
 Vendor = objItem.Vendor
Else
 On Error Resume Next
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_BaseBoard")
   For Each objItem In colItems
 Vendor = objItem.Manufacturer
 Next
End If

' ---------------------------------------------------------------------------
'                          MAC Address
' ---------------------------------------------------------------------------
Set IPConfigSet = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True")
For Each IPConfig In IPConfigSet
TempIp = Join(IPConfig.IPAddress,",")
If  TempIp <> "0.0.0.0" And Left(TempIp,7) <> "169.254" Then
MAC = IPConfig.MACAddress
MacAddr = replace(IPConfig.MACAddress,":","")
End If
Next

'----------------------------------------------------------------------------
'                    DEBUG
'----------------------------------------------------------------------------
 MsgBox        "MAC : " &  MacAddr & vbcrlf  &_
            "Vendor : " &  vendor & vbcrlf & _
            "Main Path Boot Time: " & MPBtime & " s" & vbcrlf & _ 
            "Boot Post Boot Time: " & PBtime & " s" & vbcrlf & _
            "Boot Time: " & Btime & " s" & vbcrlf & _
            "Boot Start Time : " & BStime

' ---------------------------------------------------------------------------
'                  Write datas in ForAppending mode
' ---------------------------------------------------------------------------

Set CFile = Fso.OpenTextFile (StrPATH & "\" & CsvFl, 8, true)
CFile.WriteLine(Vendor & ";" & MacAddr & ";" & MPBtime & ";" & PBtime & ";" & Btime & ";" & BStime )
'on error goto 0
CFile.close
' ------------------------------------------------------------------------------------------------------------------------

On reprend donc notre ligne de commande de base avec wevutil qu'on envoi dans un fichier temporaire :


QueryWevtutil = "cmd /c %windir%\system32\wevtutil.exe qe Microsoft-Windows-Diagnostics-Performance/Operational /rd:true /f:xml /c:1 /q:" & chr(34) & "*[System[(Level=1  or Level=2 or Level=3 or Level=4 or Level=0 or Level=5) and (EventID = 100)]]" & chr(34) &" > " & tmpFolder & "\TmpbootTime.xml"

Pourquoi ? Parce que les données obtenues n'ont pas d’entête XML.

Après traitement, on se retrouve avec un fichier exploitable par la bibliothèque Microsoft.XMLDOM
Cette bibliothèque va nous permettre de consulter les balises XML qui nous interessent.
Nous allons ainsi pouvoir récupérer les informations (entre autre) de :
  • Main Path Boot Time
  • Boot Post Boot Time
  • Boot Time
 J'y ai rajouté quelques informations comme l'adresse MAC du poste ou bien encore la marque du constructeur.

Pour des besoins de debug, j'ai ajouté un message box, mais au final, l'information que nous cherchons sera mise dans un fichier csv.

Debug :




Fichier CSV :



Aucun commentaire: