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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to Exclude a machine

Status
Not open for further replies.

dcarey230

Technical User
Feb 6, 2013
1
US
I can read VB scripting but can't write it We are having problems with a script that another person created. The script goes into each machine listed in AD and backsup All the LOGS to a share on a server. The problem we are having is that we have an ESX machine that is not windows and has no logs to pull. The script is failing giving an Access denided on line 60 of the script. We want to just be able to skip over that machine or be able to have the script move on at an err. Any ideas? THank you.

'Reads all machines in AD and backs up App, Sec and Sys logs
'On Error Resume Next

'---------Modify for each environment------------
strTopOU = "DC=AAA,DC=com" (use FQDN)
strTopDir = ""
strLogShare = "\\Computername\apps\gatheredlogs"
'------------------------------------------------

'Create a date string to use in File Names
strDate = Date
Set objRegex = new Regexp
objRegex.IgnoreCase = True
objRegex.Global = True
objRegex.Pattern = "/"
strDate = objRegex.Replace(strDate, ".")
'wscript.echo strDate

'create the FSO and create the top dir if it doesn't exist
Set objFSO = CreateObject("Scripting.FileSystemObject")
if not objFSO.FolderExists(strLogShare) then
objFSO.CreateFolder(strLogShare)
End if

'Set up the ADO connection to AD
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

'Set up the query command
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, OperatingSystem from 'LDAP://" & strTopOU & "' where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = 2 'search everything in the tree

'Execute the query
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

'Loop through the Machine Names in AD, grabbing the logs from each
Do until objRecordSet.EOF
strName = objRecordSet.Fields("Name")
'wscript.echo strName & " START"
strOS = objRecordSet.Fields("OperatingSystem")

'Create a Folder for each Machine and a folder for Today
strMachineDir = strLogShare + "\" + strName
if not objFSO.FolderExists(strMachineDir) Then
'wscript.echo "Create " & strMachineDir
objFSO.CreateFolder(strMachineDir)
end if

'Ping test each Machine
if reachable(strName) then
strTopDir = "\\" & strName & "\C$"
strTodayDir = strTopDir + "\" + strDate
if not objFSO.FolderExists(strTodayDir) Then
'wscript.echo "Create " & strTodayDir
objFSO.CreateFolder(strTodayDir)
end if

'wscript.echo strName & " Reachable!" & " " & strOS
'wscript.echo "Backing Up Logs"
BackupLog strName, "Application", strTodayDir
BackupLog strName, "Security", strTodayDir
BackupLog strName, "System", strTodayDir
''wscript.echo strTodayDir & " " & strMachineDir & "\"
'wscript.echo "Copying Folder"
objFSO.CopyFolder strTodayDir, strMachineDir & "\", True
'wscript.echo "Deleting Folder on Remote"
objFSO.DeleteFolder strTodayDir, True
else
objFSO.CreateFolder(strMachineDir + "\" + strDate)
'wscript.echo strName & "Not Online!"
objFSO.CreateTextFile(strMachineDir + "\" + strDate + "\Machine_OFFLINE_" & strDate & ".txt")
end if
objRecordSet.MoveNext
Loop

Function Reachable(strComputer)
' on error resume next

Dim WMIQuery, objWMIService, objPing, objStatus

WMIQuery = "select * From Win32_PingStatus where Address = '" & strComputer & "'"

Set objWMIPing = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIPing.ExecQuery(wmiQuery)

For each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.StatusCode<>0 Then
Reachable = False ' if computer is unreachable, return false
Else
Reachable = True ' if computer is reachable, return true
End if
Next
End Function

'Back up the Remote Event Log
Function BackupLog(strComputer, strLogName, strSaveLoc)
'wscript.echo strSaveLoc & ":" & strLogName
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate, (Backup, Security)}!\\" & _
strComputer & "\root\cimv2")
objWMIService.Security_.ImpersonationLevel = 3
Set colLogFiles = objWMIService.ExecQuery("Select * from win32_NTEventLogFile where LogFileName ='" & strLogName & "'")
For Each objLogFile in colLogFiles
'wscript.echo objLogFile.FileName
errBackupLog = objLogFile.BackupEventLog(strSaveLoc & "\" & strLogName & ".evt")
objLogFile.ClearEventLog()
'wscript.echo "--" & objLogFile.NumberOfRecords & "--" & errBackupLog
Next
End Function
 
In this case, I would check the existence of strTopDir before proceeding

Code:
'Loop through the Machine Names in AD, grabbing the logs from each
Do until objRecordSet.EOF
   strName = objRecordSet.Fields("Name")
   'wscript.echo strName & " START"
   strOS = objRecordSet.Fields("OperatingSystem")

   'Create a Folder for each Machine and a folder for Today
   strMachineDir = strLogShare + "\" + strName
   if not objFSO.FolderExists(strMachineDir) Then
      'wscript.echo "Create " & strMachineDir
      objFSO.CreateFolder(strMachineDir)
   end if

   'Ping test each Machine
   if reachable(strName) then
      strTopDir = "\\" & strName & "\C$"
      [highlight #FCE94F]If objFSO.FolderExists(strTopDir) Then[/highlight]
         strTodayDir = strTopDir + "\" + strDate
         if not objFSO.FolderExists(strTodayDir) Then
            'wscript.echo "Create " & strTodayDir
            objFSO.CreateFolder(strTodayDir)
         end if

         'wscript.echo strName & " Reachable!" & " " & strOS
         'wscript.echo "Backing Up Logs"
         BackupLog strName, "Application", strTodayDir
         BackupLog strName, "Security", strTodayDir
         BackupLog strName, "System", strTodayDir
         ''wscript.echo strTodayDir & " " & strMachineDir & "\"
         'wscript.echo "Copying Folder"
         objFSO.CopyFolder strTodayDir, strMachineDir & "\", True
         'wscript.echo "Deleting Folder on Remote"
         objFSO.DeleteFolder strTodayDir, True
      [highlight #FCE94F]End If[/highlight]
   Else
      objFSO.CreateFolder(strMachineDir + "\" + strDate)
      'wscript.echo strName & "Not Online!"
      objFSO.CreateTextFile(strMachineDir + "\" + strDate + "\Machine_OFFLINE_" & strDate & ".txt")
   end if
   objRecordSet.MoveNext
Loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top