Double line in log file error

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Daan73
Posts: 9
Joined: Thu Mar 22, 2012 9:42 pm

Double line in log file error

Post by Daan73 » Thu Mar 22, 2012 9:54 pm

hi all, i've created a script that logs if a file has the archive bit set or not on each logicaldrive of a server available First i get all logical drives of a server, then i check if the file exist, and if it exist if the archive bit is set or notthis wil be written to a csv filethe problem i have, is that the lastline of each server is writeen twice in the logsay D$ is the last drive, then the D$ check is in te log twice. with E$ as last line theits E4 log line thats written twiceI'm lost in how i can only write the last line once. as the other line of C$ doesplease helpThx Daniel the script is as follow: On Error Resume NextConst ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objShell = CreateObject("WScript.Shell") Const ADS_SCOPE_SUBTREE = 2 Const ForAppending = 8 Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim outFile If (FSO.FileExists("serversehv.txt")) then FSO.DeleteFile("serversehv.txt")end ifSet outFile = fso.OpenTextFile("serversehv.txt", ForAppending, True) Dim cn Set cn = CreateObject("ADODB.Connection") cn.Provider = "ADsDSOObject" cn.Open "Active Directory Provider" Dim cmd Set cmd = CreateObject("ADODB.Command") Set cmd.ActiveConnection = cn Dim ou ou = "OU=test,OU=test,OU=Servers,DC=eu,DC=test,DC=com" cmd.CommandText = "SELECT name " & _ "FROM 'LDAP://" & ou & "' " & _ "WHERE objectClass='computer' " & _ "ORDER BY name" cmd.Properties("Page Size") = 1000 cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE Dim rs Set rs = cmd.Execute rs.MoveFirst Do Until rs.EOF outFile.WriteLine rs(0) rs.MoveNext Loop outFile.Close wscript.echo "txtfile filled"Set outFile = Nothing Set fso = Nothing Set objTextFile = objFSO.OpenTextFile("serversehv.txt", ForReading) logfile="results.csv" Set ofile=objFSO.CreateTextFile(logfile,True) strText = objTextFile.ReadAll objTextFile.Close ofile.WriteLine ","&"Server backup Report -- Date: " & Now() & vbCrLf arrComputers = Split(strText, vbCrLF) for each item in arrcomputersSet objWMIService = GetObject _("winmgmts:" & item & "rootcimv2")Set colItems = objWMIService.ExecQuery _("Select * from Win32_LogicalDisk")For Each objItem in colItemsif objItem.DriveType = 3 thendim letterLetter = split(objItem.name, ":" )adminshare = letter(0) & "$"wscript.echo item & Adminshare end if If objFSO.FileExists("" & item & "" & adminshare & "Backup_dummy_file.txt") thenSet objFile = objFSO.GetFile("" & item & "" & adminshare & "Backup_dummy_file.txt") If objFile.Attributes AND 32 Then ofile.writeline item & " " & adminshare & "; no backup " Else ofile.writeline item & " " & Adminshare & "; backup done " objFile.attributes = objFile.attributes + 32 End Ifelseofile.writeline item & " " & adminshare & "; server or share not reachable" end ifnextnextofile.writeline ofile.writeline ","&"Server backup Report of " & item & " complete "&now() ofile.writeline

User avatar
Daan73
Posts: 9
Joined: Thu Mar 22, 2012 9:42 pm

Double line in log file error

Post by Daan73 » Thu Mar 22, 2012 9:54 pm

hi all, i've created a script that logs if a file has the archive bit set or not on each logicaldrive of a server available First i get all logical drives of a server, then i check if the file exist, and if it exist if the archive bit is set or notthis wil be written to a csv filethe problem i have, is that the lastline of each server is writeen twice in the logsay D$ is the last drive, then the D$ check is in te log twice. with E$ as last line theits E4 log line thats written twiceI'm lost in how i can only write the last line once. as the other line of C$ doesplease helpThx Daniel the script is as follow: On Error Resume NextConst ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objShell = CreateObject("WScript.Shell") Const ADS_SCOPE_SUBTREE = 2 Const ForAppending = 8 Dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim outFile If (FSO.FileExists("serversehv.txt")) then FSO.DeleteFile("serversehv.txt")end ifSet outFile = fso.OpenTextFile("serversehv.txt", ForAppending, True) Dim cn Set cn = CreateObject("ADODB.Connection") cn.Provider = "ADsDSOObject" cn.Open "Active Directory Provider" Dim cmd Set cmd = CreateObject("ADODB.Command") Set cmd.ActiveConnection = cn Dim ou ou = "OU=test,OU=test,OU=Servers,DC=eu,DC=test,DC=com" cmd.CommandText = "SELECT name " & _ "FROM 'LDAP://" & ou & "' " & _ "WHERE objectClass='computer' " & _ "ORDER BY name" cmd.Properties("Page Size") = 1000 cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE Dim rs Set rs = cmd.Execute rs.MoveFirst Do Until rs.EOF outFile.WriteLine rs(0) rs.MoveNext Loop outFile.Close wscript.echo "txtfile filled"Set outFile = Nothing Set fso = Nothing Set objTextFile = objFSO.OpenTextFile("serversehv.txt", ForReading) logfile="results.csv" Set ofile=objFSO.CreateTextFile(logfile,True) strText = objTextFile.ReadAll objTextFile.Close ofile.WriteLine ","&"Server backup Report -- Date: " & Now() & vbCrLf arrComputers = Split(strText, vbCrLF) for each item in arrcomputersSet objWMIService = GetObject _("winmgmts:" & item & "rootcimv2")Set colItems = objWMIService.ExecQuery _("Select * from Win32_LogicalDisk")For Each objItem in colItemsif objItem.DriveType = 3 thendim letterLetter = split(objItem.name, ":" )adminshare = letter(0) & "$"wscript.echo item & Adminshare end if If objFSO.FileExists("" & item & "" & adminshare & "Backup_dummy_file.txt") thenSet objFile = objFSO.GetFile("" & item & "" & adminshare & "Backup_dummy_file.txt") If objFile.Attributes AND 32 Then ofile.writeline item & " " & adminshare & "; no backup " Else ofile.writeline item & " " & Adminshare & "; backup done " objFile.attributes = objFile.attributes + 32 End Ifelseofile.writeline item & " " & adminshare & "; server or share not reachable" end ifnextnextofile.writeline ofile.writeline ","&"Server backup Report of " & item & " complete "&now() ofile.writeline

User avatar
jvierra
Posts: 13290
Joined: Tue May 22, 2007 9:57 am
Contact:

Double line in log file error

Post by jvierra » Thu Mar 22, 2012 11:17 pm

A good attempt but there are a lot of very big errors. Mostly you cannot see what is happening because you put "On Error Resume Next" at the top of the file. This should NEVER be done.


Try it this way:

uploads/2491/somecode.txt
I have also demonstrated a method for logging that is more controllable.

In this case with WMI we can use it for testing the file. Thisis actually much faster than any other method since it is direct.

We don't need to put our servers into a text file. Just use them in the record set.

We can also just enumerate the drives in a loop and use the DeviceID directly to check the file.

I ran this against a bunch of servers and it was very fast.

jvierra2012-03-23 06:30:41

User avatar
Daan73
Posts: 9
Joined: Thu Mar 22, 2012 9:42 pm

Double line in log file error

Post by Daan73 » Fri Mar 23, 2012 12:46 am

Found the echo , didn't look right, but then i'm starring at mine and yours all morning already :-PWhat i have now is that some servers in the OU are not onlinewich result in a script error remote machine does not exist , at this point it should log that server is not available. how can i acomplish thatWhen i check a file if archivebit is set or not , in you script i think its the file.Archive part, archivebit has to be set again for the next backup roundwhere do i put that and how ?thx again for your time and expertise
Daan732012-03-23 07:48:29

User avatar
jvierra
Posts: 13290
Joined: Tue May 22, 2007 9:57 am
Contact:

Double line in log file error

Post by jvierra » Fri Mar 23, 2012 1:46 am

CSCRIPT //H:cscript

This will switch the defaul engine to not use the message boxes.

User avatar
Daan73
Posts: 9
Joined: Thu Mar 22, 2012 9:42 pm

Double line in log file error

Post by Daan73 » Mon Mar 26, 2012 12:31 am

Thx got the part for error resume next , its working like a charm on every part but one i can't to worki want to set the archive bit again on the file , after the log writes that backup is done successfull , as there is no archive bitI tried it in the following code snap from the code you gave meIf files.Count > 0 Then For Each file in files If file.Archive Then WriteLog server, disk.DeviceID, "not backed up" Else WriteLog server, disk.DeviceID, "backed done" Set objFile = objFSO.GetFile("" & server & "" & disk.DeviceID & "$" & "Backup_dummy_file.txt") objFile.attributes = objFile.attributes + 32It does't seems to set archivebit againCan you help me with this last partthx very much for your time so far

User avatar
jvierra
Posts: 13290
Joined: Tue May 22, 2007 9:57 am
Contact:

Double line in log file error

Post by jvierra » Mon Mar 26, 2012 3:49 am

You are thinking backwards. The archoive bit says the file has not been backed up. Changiing this will not cause the file to be backed up. it wil cause it to be skipped.


The backup system manages the Archive bit on files and folders.



User avatar
Daan73
Posts: 9
Joined: Thu Mar 22, 2012 9:42 pm

Double line in log file error

Post by Daan73 » Mon Mar 26, 2012 5:13 am

Yes i knowbut the file never changes , so in the next backupround it will not clear the archive bit as its clear already, so after it logs that the archive bit isn't therei want to set it, so the backupsoftware can remove it again, as it touches the file.That why i need the bit set againsorry i didn't make it clear enough

User avatar
Daan73
Posts: 9
Joined: Thu Mar 22, 2012 9:42 pm

Double line in log file error

Post by Daan73 » Mon Mar 26, 2012 8:45 am

what i want to acomplish is that to check if the backupsoftware did backup the drive of the server , or actually if it has a backup schedule in backupexecas i have to many policy's and configs in bckupexec, i need to know if a newbuild server , is added to a backup policy in backupexec.my idea was to create the dummy file and set the archive bit after a backup is checked . So that if backupexec clears the bit i know for sure that the server is in one of the many backup policy'sso i want to backup the file every day again, so whenever a new server is build by a collegea that he didn't forget to add it to backupexec or netbackup

User avatar
jvierra
Posts: 13290
Joined: Tue May 22, 2007 9:57 am
Contact:

Double line in log file error

Post by jvierra » Mon Mar 26, 2012 9:21 am

Creating a new file alwyas sets the Archive bit. YOu don't need to set it.

Locked