Solved

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

Posted on 2015-02-01
7
91 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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
A brief introduction to what I consider to be the best editor for PowerShell.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

14 Experts available now in Live!

Get 1:1 Help Now