Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-01
7
Medium Priority
?
98 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 10

Assisted Solution

by:Benjamin MOREAU
Benjamin MOREAU earned 400 total points
ID: 40582924
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
ID: 40582935
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
ID: 40582983
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Expert Comment

by:footech
ID: 40582989
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 1600 total points
ID: 40583005
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
ID: 40583023
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
ID: 40583031
Actually Subsun's script works perfectly.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

886 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