How to gather lists of files from a particular directory from all machines in a domain.

I am looking for help on how to automate the process of checking the contents of certain directories on all machines on my network.
I have tried doing this with a bat file but know that with VB script I have many more options, I just need to be pointed in the the right direction.
Lets start with an example of what I would like to try to do.  I have 3 computers all on the same domain, I am using Machine 1 and want to check these certain directory on Machine 2 and Machine 3.  I am logged into Machine 1 as a Domain Admin so I would have access to Machines 2 and 3.  Understood that a firewall might be active to prevent access on 2 and 3.
I want to capture the list (dir) of  "c:\windows\a\" and "c:\windows\b\"  with logic to check for "winnt" if that exists instead of windows.

Currently, if I use the dos command "dir \\machine2\c$\windows\a \\machine2\c$\windows\b \\machine3\c$\windows\a > c:\capture.csv" I get what I want.
but.......

Issues are:
1) If a machine3  has a firewall it stops and I get nothing in my file. Would like the script to keep running and give a message of "not able to access"
2) For a whole network of machines my command gets too long.
3) I have to type in every machine, would be much easier to import a machine list or have them querried.
4) I need to specify the windows vs winnt directory, would like to have it check first  and use the correct directory.

Why?  I had a malware outbreak and am pretty sure I have things cleaned up.  My virus scanners don't pickup all of the exe files as "bad" so if I could run a script that automates the above I could just check my csv, or txt dump and would know what work I have left to do if any.

I have written basic scripts to install programs through login.bat but would like to grow a bit and try building a tool rather than going the manual route.
Any help /  pointers / links  to get me moving in the right direction would be appreciated.  
Thanks in Advance
KC
Kenny JohnsonIT Support StaffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
You could try adding the machines to an array,then loop through the array calling your dir command. Here is a basic example. Add the following to a .vbs file and adjust your path's accordingly:


Dim WshShell,x,fso,WinFolder
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

Dim Machines(2)
Machines(0) = "\\machine1\c$\"
Machines(1) = "\\machine2\C$\"
Machines(2) = "\\machine3\C$\"

For x = Lbound(Machines) to Ubound(Machines)
   If x = Lbound(Machines) Then
      if fso.FolderExists(Machines(x) & "Windows") Then
         WinFolder = "Windows"
      End if
      if fso.FolderExists(Machines(x) & "Winnt") Then
         WinFolder = "Winnt"
      End if
      If WinFolder = "" Then
         MsgBox "Windows Folder Not Found!"
         Exit For
      End if
      ' first entry so overwrite file
      WshShell.Run "%COMSPEC% /c dir " & Machines(x) & WinFolder & "\a > C:\Capture.csv",0,True
      ' append to file
      WshShell.Run "%COMSPEC% /c dir " & Machines(x) & WinFolder & "\b >> C:\Capture.csv",0,True
Else
      ' append to file
      WshShell.Run "%COMSPEC% /c dir " & Machines(x) & WinFolder & "\a >> C:\Capture.csv",0,True
      WshShell.Run "%COMSPEC% /c dir " & Machines(x) & WinFolder & "\b >> C:\Capture.csv",0,True
   End If
Next

MsgBox "Done!"

Set fso = Nothing
Set WshShell = Nothing
Kenny JohnsonIT Support StaffAuthor Commented:
Vinny,
Thanks for the quick and incredibly rich help...  I am begining to work with that script now and will get back to you shortly....
Cheers
KC
Kenny JohnsonIT Support StaffAuthor Commented:
Vinny,

I have tinkered with this and have partial success.

The machines that have the "Winnt" directory work fine. (exactly what I want to see...)
Machines with "WINDOWS" folder do not (2003 or XP)  they do work if I hit them from "start > run > \\Machine\c$"
The CSV file shows nothing about the machine with the Windows directory.  I tried  them in position (1) and (2) it just got completely skipped.

KC
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

vinnyd79Commented:
Do the machines with the Windows directory also have a Winnt directory?
Kenny JohnsonIT Support StaffAuthor Commented:
no they don't, and I see what the problem is but again don't know enough about how to fix..

If I put the 2003 server (with the windows directory) first, my CSV file only has "windows" directory machines listed in it .
So, whatever windows system folder is in the first position, that's the only directory it works with.....
vinnyd79Commented:
Sorry, I screwed that up. I'll fix it in a sec.....
vinnyd79Commented:
Had the winfolder check in the wrong spot:


Dim WshShell,x,fso,WinFolder
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

Dim Machines(2)
Machines(0) = "\\machine1\c$\"
Machines(1) = "\\machine2\C$\"
Machines(2) = "\\machine3\C$\"

For x = Lbound(Machines) to Ubound(Machines)

      if fso.FolderExists(Machines(x) & "Windows") Then
         WinFolder = "Windows"
      End if
      if fso.FolderExists(Machines(x) & "Winnt") Then
         WinFolder = "Winnt"
      End if
      If WinFolder = "" Then
         MsgBox "Windows Folder Not Found!"
         Exit For
      End if


   If x = Lbound(Machines) Then
      ' first entry so overwrite file
      WshShell.Run "%COMSPEC% /c dir " & Machines(x) & WinFolder & "\a > C:\Capture.csv",0,True
      ' append to file
      WshShell.Run "%COMSPEC% /c dir " & Machines(x) & WinFolder & "\b >> C:\Capture.csv",0,True
Else
      ' append to file
      WshShell.Run "%COMSPEC% /c dir " & Machines(x) & WinFolder & "\a >> C:\Capture.csv",0,True
      WshShell.Run "%COMSPEC% /c dir " & Machines(x) & WinFolder & "\b >> C:\Capture.csv",0,True
   End If
Next

MsgBox "Done!"

Set fso = Nothing
Set WshShell = Nothing

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kenny JohnsonIT Support StaffAuthor Commented:
Vinny,

Cha Ching.  You made that look easy!  Appreciated.

Any thoughts on a way to take a list of computers and automate the Dim Machine (x) list (or something similar)?

Thanks for taking the time...  Very nice of you.

KC
vinnyd79Commented:
It would be easy to read the machine data from a file instead. This example assumes the path for each machine is on it's own line in a file without the trailing backslash. If you have a file and need to parse the line for the data let me know.
Also this is not tested because I don't have any other machines available to use as a test so let me know if it doesn't work.


This example assumes the file is like this:
"\\machine1\c$"
"\\machine2\c$"
"\\machine3\c$"



' script
Dim WshShell,x,fso,WinFolder,fil,Machine
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

' open textfile in read mode
Set fil = fso.OpenTextFile("C:\MachineList.txt",1,True) ' 1 = For reading

x = 0 ' x is now the counter so you can use > on the first entry to overwrite the file

' loop through file
While Not fil.AtEndOfStream

' read line
Machine = fil.ReadLine
x = x + 1

if fso.FolderExists(Machine & "\Windows") Then
    WinFolder = "Windows"
End if

if fso.FolderExists(Machine & "\Winnt") Then
    WinFolder = "Winnt"
End if

If WinFolder = "" Then
    MsgBox "Windows Folder Not Found!"
    Exit For
End if


If x = 1 Then
     ' first entry so overwrite file
     WshShell.Run "%COMSPEC% /c dir " & Machine & "\" & WinFolder & "\a > C:\Capture.csv",0,True
     ' append to file
     WshShell.Run "%COMSPEC% /c dir " & Machine & "\" & WinFolder & "\b >> C:\Capture.csv",0,True
Else
     ' append to file
     WshShell.Run "%COMSPEC% /c dir " & Machine & "\" & WinFolder & "\a >> C:\Capture.csv",0,True
     WshShell.Run "%COMSPEC% /c dir " & Machine & "\" & WinFolder & "\b >> C:\Capture.csv",0,True
End If

' get next line
Wend

MsgBox "Done!"

fil.Close
Set fil = Nothing
Set fso = Nothing
Set WshShell = Nothing

vinnyd79Commented:
Just noticed something. Make sure you take out the "Exit For" line since there is no For/Next loop in that version.
Kenny JohnsonIT Support StaffAuthor Commented:
I will give it a shot this weekend if I can get the other weekend projects cleared off, otherwise Monday.
Thanks for additional help.

By the way, I ran the other script today and revealed 2 machines I missed.  Really handy.

Have a good weekend.

KC
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.