Solved

How to find if a remote computer has a file in AD

Posted on 2015-02-01
7
90 Views
Last Modified: 2015-02-01
I am trying to use powershell to see if a remote computer has a file present. I am very new to powershell. Is there a way to say is c:\test.txt present on computer computer01?
0
Comment
Question by:stakor
7 Comments
 
LVL 9

Assisted Solution

by:Benjamin MOREAU
Benjamin MOREAU earned 100 total points
Comment Utility
Something like this :
$server=Myserver

if(Test-Path \\$server\folder\filetocheck.txt){ $server"/yes`n" | out-file c:\log.txt -append}

else{$server"/no`n" |out-file c:\log.txt -append}
1
 

Author Comment

by:stakor
Comment Utility
I am getting the following error:

Unexpected token '/yes
' in expression or statement.
At line:2 char:45

Open in new window

0
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Slightly modified version of above code.. but this will work, only if the admin share is enabled on the remote computer.
$server=Myserver
$(if (Test-Path \\$server\folder\filetocheck.txt){ "$server/yes`n" } else {"$server/no`n"}) >> c:\temp\log.txt

Open in new window

1
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Expert Comment

by:footech
Comment Utility
If you know the exact path of the file, the method that Benjamin and Subsun showed is what I'd use.

Another method to use, when you may not know the full path of the file or perhaps there are multiple matches, is with WMI.
Get-WmiObject Cim_DataFile -Filter "extension = 'txt' and filename = 'test' and drive = 'c:'" -computername "server"

Open in new window

0
 
LVL 40

Accepted Solution

by:
Subsun earned 400 total points
Comment Utility
For instance if you have to check multiple servers. You can input the server names in C:\server.txt and after completing the script the results will be saves to a csv file.
GC C:\server.txt | %{
	$server = $_
	If (Test-Path \\$server\C$\test.txt){
			New-Object PSOBject -Property @{
			Server = $server
			Status = "Yes"
			}
		}else {
		New-Object PSOBject -Property @{
		Server = $server
		Status = "No"
		}
	}
}| Export-Csv C:\temp\report.csv -nti

Open in new window

1
 

Author Comment

by:stakor
Comment Utility
I got it to work by spacing the $server and adding a +. I will give credit to the first answer.

if(Test-Path \\$server\c$\test.txt){ $server + "/yes`n" | out-file c:\log.txt -append}
else{$server + "/no`n" |out-file c:\log.txt -append}
0
 

Author Comment

by:stakor
Comment Utility
Actually Subsun's script works perfectly.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now