[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 561
  • Last Modified:

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.
0
VioCH
Asked:
VioCH
  • 2
  • 2
1 Solution
 
NavdeepCommented:
Hi,

Which OS do you have?

Also can you come up with some draft version of the script and then I can review it.

Here is the logic to check whether server is online or not
# Logic to check whether host exist and is online or not

function checkSystemAvailability ([string] $comp) {
Try
{
$IPres = [System.Net.Dns]::GetHostAddresses($comp)

}
Catch [System.Exception]
{
$RegResult.Err2 = "HostNotValid"
return $RegResult
break
}

if ($IPres -eq "")
{
Try
{
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($comp)
}
Catch [System.Exception]
{
[System.Exception]
$RegResult.Err2 = "HostNotOnline"
return $RegResult
break
}
}
}

Regards,
Navdeep [v-2nas]
ExchangeADTech
0
 
VioCHAuthor Commented:
Thanks for answering my question.

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

Can you come up with some draft code?

Regards,
Navdeep [v-2nas]
ExchangeADTech
0
 
VioCHAuthor Commented:
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
}
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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