Check path, audit the path if it got files write-host.

Dear expert

I would like to build a script that do the server audit like this:
$Servers = Get-Content C:\temp\serverlist.txt
$result = Foreach ($server in $Servers) { gc "$server"       
}
if ($server-ne $null){write-host "File in $Server"}
}
The operation is simply check the server list in the txt files, if there is any files under the path list, write-host file in path.
Code above is not working as planned, the result always show the last line of the serverlist txt got a file in it and I checked, there is no files.
Any ideas?
LVL 1
WeTiAsked:
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.

oBdACommented:
If you just want to check for the existence of files, don't use gc (Get-Content); gci (Get-ChildItem is better suited.
On that same topic, don't use aliases in scripts. They're fine in the command line for a quick interactive one-liner, but not in scripts. Take the time to write out the full cmdlet name.
Anyway, I'm assuming from your former question (which has an an utterly non-functional solution - you should maybe have a moderator review and re-open delete it), that you have a file full of lines like the following:
\\srvp03\logs\log*
This should do the trick:
Get-Content -Path C:\temp\serverlist.txt | Where-Object {Get-ChildItem -File -Path $_} | ForEach-Object {Write-Host "File in $($_)"}

Open in new window

0

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
WeTiAuthor Commented:
Works perfectly! Thanks
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.