Solved

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

Posted on 2015-02-01
7
94 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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 40

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

728 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