Reputation: 1
I am trying to develop a VBS script which creates backups with shadowcopy.exe
and copies them with rsync to a specific destination.
There are log files which will be created for the rsync copy procedure by the same script and getting
checked for specific words (like "error" or "failed").
The problem I have now is that I don't know how to check the log files/read out the specific keywords.
I read that InStr
is a possibility to solve my problem, but I dont't know how to use it to define my keywords, read them out and combine it with my mail alert. Maybe someone has an advice for me?
My code so far:
Dim oEmail, oConf
Dim authuser, authpass, smtpserver
Dim recipient,sender,subject,message
Dim ObjFso
Dim FIL_LOG
Dim response
'Angaben zur Quelle und Ziel
quelle = "c:\vshadow" 'beispiel c:\vshadow
ziel = "/cygdrive/c/users/example/desktop/test/" 'beispiel /cygdrive/c/users/example/desktop/test/
'angaben zu den emails die verschickt werden wollen
authuser = "[email protected]"
authpass = "123"
smtpserver = "server"
recipient = "[email protected]"
sender = "[email protected]"
subject = "Test E-Mail"
message1 = "Schattenkopie konnte nicht erstellt werden"
message2 = "rsync befehl ist durchgelaufen"
message3 = "rsync ist abgebrochen"
'für den email versand benötigte daten
Set oEmail = CreateObject("CDO.Message")
Set oConf = CreateObject("CDO.Configuration")
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = authuser
oConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = authpass
oConf.Fields.Update
'Schattenkopie erstellen
Const CONTEXT = "ClientAccessible"
Set Args = WScript.Arguments
If Args.Count() > 0 Then
VOLUME = Args.item(0)
Else
VOLUME = "C:\"
End If
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objShadowStorage = objWMIService.Get("Win32_ShadowCopy")
errResult = objShadowStorage.Create(VOLUME, CONTEXT, strShadowID)
'falls schattenkopie nicht erstellt werden kann email mit Fehlermeldung, sonst gehts
'weiter
If errResult <> 0 Then
oEmail.Configuration = oConf
oEmail.To = recipient
oEmail.From = sender
oEmail.ReplyTo = sender
oEmail.Subject = subject
oEmail.HTMLBody = message1
End If
'Mounted die Schattenkopie und Kopiert per Rsync
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%COMSPEC% /C ShadowSpawn.exe " & quelle & _
" t: rsync -rltDv --log-file=rsync.log--link-dest=/" & ziel & _
"/%last_run% /cygdrive/t/* " & ziel & _
" > c:\users\example\desktop\vbs\rsync\bin\vss1.log ", 0, True
'''''''''''''''''''''
'Hier möchte ich die gerade erstelle log Datei auf Stichwörter wie z.B error
'untersuchen. Falls error in der log datei vorhanden ist soll es mir eine Abbruch
'mail schicken. Ansonsten eine email das alles gut durchgelaufen ist
ergenbis = InStr(1, vss1, error, 1)
'If Ergebnis <> 0 Then 'Email verschicken das alles durchgelaufen ist
oEmail.Configuration = oConf
oEmail.To = recipient
oEmail.From = sender
oEmail.ReplyTo = sender
oEmail.Subject = subject
oEmail.HTMLBody = message2
Else 'abbruchmail verschicken
oEmail.Configuration = oConf
oEmail.To = recipient
oEmail.From = sender
oEmail.ReplyTo = sender
oEmail.Subject = subject
oEmail.HTMLBody = message3
End If
oEmail.Send
Set oConf = Nothing
Set oEmail = Nothing
Upvotes: 0
Views: 96
Reputation: 38765
Use Option Explicit
to avoid blunders like
ergenbis = instr(1, vss1, error, 1)
if Ergebnis <> 0 then 'Email verschicken das alles durchgelaufen ist
Think about the difference between name (variable, file) and content (variable, file).
Write a simple test script that applies InStr() to a file/each line of a file
Upvotes: 1