Solved

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

Posted on 2015-02-01
7
92 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 39

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 400 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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