Link to home
Start Free TrialLog in
Avatar of VioCH
VioCHFlag for Switzerland

asked on

Powershell Script: Search 3 files on all active Windows Servers within a Domain

I'm looking for a VBS Script or Windows Powershell Script how does do the following:

1. Search per LDAP all Windows Servers within a domain.

2. Pings' the found Windows Servers.
     and writes depending on success result or not
     2 files Servers.txt & ServersInactive.txt with the Servername.

3. Read the Servers.txt and search's through all server in the list for
     all admin-share's (c$,d$, etc) and searches in this shares for 3 files
     (abc.txt,def.txt,ghi.doc) and writes the result into a file output.txt
     in the following format:
     Date&Time, Servername, Path, filename.
ASKER CERTIFIED SOLUTION
Avatar of Navdeep
Navdeep
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VioCH

ASKER

Thanks for answering my question.

I do have Windows Server from 2003 sp2 and 2008 R2 sp1.
Great, you can use powershell to do this job.

Can you come up with some draft code?

Regards,
Navdeep [v-2nas]
ExchangeADTech
Avatar of VioCH

ASKER

Here is my draft code. I did solve it a bit differently, but I can use your functions above in a different project.

My future work I have to do is, solving long paths (Fullname < 240)  and iterate the whole c$,d$.. thing.

# Reads a list of computers names from computer.txt file.
# loop through each computer name
cls
Clear-Content "results.txt"
Get-Content "Servers.txt" | ForEach-Object {

  # ping Servers to see if it is online
  # Test-Connection is WMI Ping, and $_ is the current computer,
  # the current object in the pipeline
  If (Test-Connection $_ -Count 1 -Quiet) {

    # This is a bit slow
    # search the base drive for a specific file
    # \\somecomputer\c$\somefile.txt   {recursively through all folders}
    # ForEach-Object, just in case you have more than one match

      $Include=@("CONFIG.SYS","AUTOEXEC.BAT")
      If ((Test-Path -Path "\\$_\c$" )) {
       Get-ChildItem "\\$_\c$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\d$" )) {
       Get-ChildItem "\\$_\d$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\e$" )) {
       Get-ChildItem "\\$_\e$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\f$" )) {
       Get-ChildItem "\\$_\f$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\g$" )) {
       Get-ChildItem "\\$_\g$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\h$" )) {
       Get-ChildItem "\\$_\h$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\i$" )) {
       Get-ChildItem "\\$_\i$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\j$" )) {
       Get-ChildItem "\\$_\j$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\k$" )) {
       Get-ChildItem "\\$_\k$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\l$" )) {
       Get-ChildItem "\\$_\l$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\m$" )) {
       Get-ChildItem "\\$_\m$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\n$" )) {
       Get-ChildItem "\\$_\n$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\o$" )) {
       Get-ChildItem "\\$_\o$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\p$" )) {
       Get-ChildItem "\\$_\p$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\q$" )) {
       Get-ChildItem "\\$_\q$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\r$" )) {
       Get-ChildItem "\\$_\r$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\s$" )) {
       Get-ChildItem "\\$_\s$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\t$" )) {
       Get-ChildItem "\\$_\t$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\u$" )) {
       Get-ChildItem "\\$_\u$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\v$" )) {
       Get-ChildItem "\\$_\v$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\w$" )) {
       Get-ChildItem "\\$_\w$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\x$" )) {
       Get-ChildItem "\\$_\x$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\y$" )) {
       Get-ChildItem "\\$_\y$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      If ((Test-Path -Path "\\$_\z$" )) {
       Get-ChildItem "\\$_\z$" -Include $Include -Recurse -Force -ErrorAction  SilentlyContinue |
      Foreach-Object -process { $_.Fullname } | Add-Content "results.txt"
      }
      

  # End of If for Ping
  }

# End of computer loop
}