We help IT Professionals succeed at work.

Powershell to find if a file exist on 20 servers

178 Views
1 Endorsement
Last Modified: 2018-10-03
Hi All,

I have about 20 Windows OS servers and I need a powershell script that read a text file that contains a list of server names, then go into each server and find out if a particular file exists, if it does, write the host name to fileexsit.txt and it the file doesn't exist, write to a file named filenotexist.txt.

Please help!!


Thanks!
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
MichelangeloSystem Administrator / Postmaster
CERTIFIED EXPERT

Commented:
Something along these lines. Note that myhostname is a fqdn list of hosts one for each row (no header)
I’m writing from memory so syntax may be imprecise.
  $filename = C:\filename
Get-content myhostnames.txt | % {

$result =    Invoke-command -computername $_  -scriptblock { if ( test-file $using:filename ) { $true} else { $false } }
If ( $result ) { $_ >> fileexists.txt} else { $_.name >> fileNOTexists.txt}
}
MichelangeloSystem Administrator / Postmaster
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions