• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

San an IP range and directory's on the hosts for *.mdf

I need to:

 scan an ip range:xxx.xxx.xxx.1-xxx.xxx.xxx.254
Resolve the hostname to IP
scan the host for on all drives for *.mdf
and output to a txt or csv file.

Can anybody help me ???

  • 2
1 Solution
"nmap" will scan a local are network for addresses and ports. -> http://www.insecure.org/

"PsExec" in PsTools from www.sysinternals.com will let you execute commands on a remote host to discover files.

You just have to script them together to provide directory listings for all .mdf files on all hosts on the lan

The following script will do the job, but if there are IP addresses within the range being scanned for that can't be located the script will run slow.  It has to await a "connection timeout" in order to determine the IP isn't reachable before moving to the next one in the range.

' scan an ip range:xxx.xxx.xxx.1-xxx.xxx.xxx.254
' Resolve the hostname to IP
' scan the host for on all drives for *.mdf
' and output to a txt or csv file.
' Note:  The statement at line # 52 executes "very slowly" when the
'        IP address isn't found.  Apparently, WMI waits for a timeout
'        from the Operating System before producing an error... that's
'        why the "on error..." logic surrounds it.
'        I have found no faster way as of yet to determine if the IP
'        address is valid before making this call.
Option Explicit

Dim objWMIService
Dim objFSO
Dim strExtension
Dim FileCount
Dim strHostName
Dim objOutputFile


Sub Main()
Dim colSettings
Dim objComputer
Dim arrDrives
Dim SubFolder
Dim objRootFolder
Dim d
Dim OutputFile
Dim strSubNet
Dim intAddr
Dim strAddr
Dim strIPaddr

   strSubNet    = "192.168.11"      ' replace with your values...
   intAddr      = 1
   strExtension = ".mdf."
   OutputFile   = "C:\MDBlist.txt"  ' replace with your values...

   Set objFSO        = CreateObject("Scripting.FileSystemObject")
   Set objOutputFile = objFSO.CreateTextFile(OutputFile)

   While intAddr < 254
      strIPaddr = strSubNet + "." & intAddr

      On Error Resume Next

      ''' Next line causes error (and slow operation) when IP address isn't found
      Set objWMIService = GetObject("WinMgmts:{impersonationlevel=impersonate}!\\" & strIPaddr & "\root\cimv2")

      If Err.Number <> 0 Then
         objOutputFile.WriteLine strIPaddr & " Not Found."
         On Error Goto 0
         Set colSettings = objWMIService.ExecQuery("Select * From Win32_ComputerSystem")

         For Each objComputer In colSettings
            FileCount = 0
            strHostName = objComputer.Name

            Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strHostName & "\root\cimv2")
            arrDrives = DrivesToScan()

            For d = 0 To UBound(arrDrives)
               If objFSO.FileExists(arrDrives(d) & ":\nul") Then
                  Set objRootFolder = objFSO.GetFolder(arrDrives(d) & ":\")
                  For Each SubFolder In objRootFolder.SubFolders
                     If Mid(SubFolder.Path,4) <> "System Volume Information" Then
                        ScanFiles SubFolder
                        ScanFolders SubFolder
                      End If
               End If

      End If

      intAddr = intAddr + 1


End Sub

Sub ScanFolders(Folder)
Dim SubFolder

   If Folder <> "" Then
      For Each SubFolder In Folder.SubFolders
          ScanFiles SubFolder
      ScanFolders SubFolder
   End If

End Sub

Sub ScanFiles(Folder)
Dim colFiles
Dim objFile
Dim s2

   Set colFiles = Folder.Files
   For Each objFile In colFiles
      If InStr(strExtension, "." & objFSO.GetExtensionName(objFile) & ".") Then
         s2 = "\\" & strHostName & "\" & Replace(Folder.Path, ":", "$") & "\" & objFile.Name
         objOutputFile.WriteLine s2
         FileCount = FileCount + 1
      End If

End Sub

Function DrivesToScan()
Dim wmiDiskDrives
Dim wmiDiskDrive
Dim strEscapedDeviceID
Dim wmiDiskPartitions
Dim wmiDiskPartition
Dim wmiLogicalDisks
Dim wmiLogicalDisk
Dim strDriveLetter
Dim strDrives

   strDrives = ""

   Set wmiDiskDrives = objWMIService.ExecQuery("SELECT Caption, DeviceID FROM Win32_DiskDrive")

    For Each wmiDiskDrive In wmiDiskDrives
      'Backslash in disk drive deviceid must be escaped by "\"
       strEscapedDeviceID = Replace(wmiDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare)

      'Use the disk drive device id to find associated partition
      Set wmiDiskPartitions = objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" _
            & strEscapedDeviceID & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition")

      For Each wmiDiskPartition In wmiDiskPartitions
         'Use partition device id to find logical disk
         Set wmiLogicalDisks = objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
               wmiDiskPartition.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition")

         For Each wmiLogicalDisk In wmiLogicalDisks
            strDriveLetter = UCase(Left(wmiLogicalDisk.DeviceID, 1))
         '''If InStr("C", strDriveLetter) Then
               strDrives = strDrives & strDriveLetter & " "
         '''End If

   DrivesToScan = Split(Trim(strDrives))

End Function  

Drew, did you have an opportunity to run the script I provided?  Did it work for you?

-- Lynn

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now