?
Solved

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

Posted on 2005-03-01
3
Medium Priority
?
262 Views
Last Modified: 2013-11-15
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 ???

Thanks,
0
Comment
Question by:Drew_Mora
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:gabeso
ID: 13432455
"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

0
 
LVL 22

Expert Comment

by:JesterToo
ID: 13436590
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

   Main()

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
      Else
         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
                   Next
               End If
            Next
         Next

      End If

      intAddr = intAddr + 1
   Wend

   objOutputFile.Close

End Sub

Sub ScanFolders(Folder)
Dim SubFolder

   If Folder <> "" Then
      For Each SubFolder In Folder.SubFolders
          ScanFiles SubFolder
      Next
      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
   Next

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
         Next
      Next
   Next

   DrivesToScan = Split(Trim(strDrives))

End Function  

HTH,
Lynn
0
 
LVL 22

Accepted Solution

by:
JesterToo earned 2000 total points
ID: 13514443
Drew, did you have an opportunity to run the script I provided?  Did it work for you?

-- Lynn
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
Simple Linear Regression

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question