Powershell to find if a file exist on 20 servers

fireburn11
fireburn11 used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
This will use the UNC and create three files, the third one for files where the network share wasn't accessible (server turned off, firewall issue, permission issue, ...).
$localFilePath = 'C:\Temp\FindMe.txt'
$fileExists = 'C:\Temp\fileexist.txt'
$fileMissing = 'C:\Temp\filemissing.txt'
$fileError = 'C:\Temp\fileerror.txt'

$fileExists, $fileMissing, $fileError | Remove-Item ErrorAction SilentlyContinue
$drive = Split-Path -Path $localFilePath -Qualifier
Get-Content -Path C:\Temp\servers.txt | ForEach-Object {
	Write-Host "Processing $($_)"
	$share = "\\$($_)\$($drive.Replace(':', '$'))"
	If (Get-Item -Path $share -ErrorAction SilentlyContinue) {
		If (Test-Path "\\$($_)\$($localFilePath.Replace(':', '$'))") {
			$_ | Add-Content -Path $fileExists
		} Else {
			$_ | Add-Content -Path $fileMissing
		}
	} Else {
		"$($_): Unable to access '$($share)'!" | Add-Content -Path $fileError
	}
}

Open in new window

MichelangeloSystem Administrator / Postmaster

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
Commented:
Replace
 test-path with test-file... cannot edit from mobile sorry

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial