Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

search servers for a specific folder, build a list, use robocopy to move the data, and then delete

Posted on 2009-04-04
6
Medium Priority
?
694 Views
Last Modified: 2013-11-09
Hello, I would like to thank all those who have posted here in the past. I have been able to put together many scripts that I need using parts from the post here.  I have come to a point that I am stuck on two projects. Any help would be great.
Here is what I need to do using VBS or WMI.  I need to search two specific paths on all the servers in our network, the server names are in a text file (server.txt) for a list of folders names (foldernames.txt). The path are fixed path:  (example \\srvname\sharename\folder1) If
If the specific folder is there, build another text file for the server names and folders. Once the folder name list and server search is complete, create a target folder on a specific server (example: \\archivesrv\foldername).
Once the sources are known and the target folder created, I want to use robocopy to run a move of the data from the source to the target creating a log file of the data. One of the issues I have is running a move job with robocopy on the same foldername from two sources at the same time.

Any ideas would be great
0
Comment
Question by:philwitkowski
[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
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:Sander Stad
ID: 24075414
I created a little script for you to do this. What is does at first is get all the values in the textfiles. I assumed that the folders and the servers are separated by a new line for every item.

It than checks through a ping command if the server is still online. If it is the script continues. It than get all the folders that have the servername in it and puts these in an array. The script loops through the array to see if the folder exists. if is does it creates the new paths and copies the folder to the new location with robocopy.

Here is the script

# Standard variables
$logdir 	= "E:\TEST\logfiles"
$archive 	= "E:\TEST\archive"
$serverList 	= "E:\TEST\servers.txt"
$folderList 	= "E:\TEST\folders.txt"
 
# Get all the servers and folders from the textfiles
$arrServers = Get-Content $serverList
$arrFolders = Get-Content $folderList
 
#Function Write-Logfile
#
# Writes a logfile in the given logdir. Creates a logfile per servername
# and writes the message.
#
# Param
#	$logdir: 	Logdirectory for the logfiles
#	$server: 	Servername used for the logfile
#	#message:	Message that need to be written
Function Write-Logfile([string]$logdir, [string]$server, [string]$message){
	$date = Get-Date -format "yyyyMMdd"
	$logfile = $logdir + "\" + "logfile" + "_" + $date + "_" + $server + ".log"
	
	Add-Content $logfile "$message"
}
 
# Loog through the servers
Foreach($server In $arrServers){
	# Create a ping object to see if the server is online
	$ping = New-Object System.Net.NetworkInformation.Ping
	$reply = $ping.send($server)
	
	# If the server gives a reply
	If($reply.status -eq "Success"){
		# Write to logfile
		Write-Logfile $logdir $server "[$server]"
		
		# Get all the folders with the servername
		$folders = $arrFolders -match $server
		
		# Loop through all the folders 
		Foreach($folder In $folders){
			# Check if the folder exists
			If(Test-Path $folder){
				# If it does exist write to logfile
				Write-Logfile $logdir $server $folder
				# Create the new path for the files
				$serverArchive = $archive + "\" + $server
				# Test if the path exists
				If(!(Test-Path $serverArchive)){
					New-Item $serverArchive -Type directory
				}
				# Copy the items
				robocopy $folder $serverArchive
				
			}
		}
	}
}

Open in new window

0
 

Author Comment

by:philwitkowski
ID: 24078824
sstad,
First off, thanks for the post! After installing power shell on a XP machine, I ran the script. Updated for local information of course.  I do not see any errors but on have the server name posted to the log file. I tried adding sharename\folder and \sharename\folder in the folders.txt file with no luck. Am I missing a step?
0
 
LVL 9

Expert Comment

by:Sander Stad
ID: 24084204
My folders.txt looked like this:

\\server1\folder1
\\server2\folder2
\\server3\folder3
\\server4\folder4


0
Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

 

Author Comment

by:philwitkowski
ID: 24119118
sstad,
I see what is happening, my result is the share folder name at the root of the server.  What I am looking to do is search all the server names for a folder in the specific subfolder.  For example, I want to search for a folder called "prj1234" (I could have a lot of them) on servers named: "server1, server2,server3, server4, etc" that are in a share called "data" If on any of the servers, make a list and run robocopy to copy "prj1234" from each server to the archive path. (one server at a time.)
0
 
LVL 9

Accepted Solution

by:
Sander Stad earned 1200 total points
ID: 24136780
If the data folder is always static you can do this:

# Standard variables
$logdir         = "E:\TEST\logfiles"
$archive        = "E:\TEST\archive"
$serverList     = "E:\TEST\servers.txt"
$folderList     = "E:\TEST\folders.txt"
$subfolder      = "data"
 
# Get all the servers and folders from the textfiles
$arrServers = Get-Content $serverList
$arrFolders = Get-Content $folderList
 
#Function Write-Logfile
#
# Writes a logfile in the given logdir. Creates a logfile per servername
# and writes the message.
#
# Param
#       $logdir:        Logdirectory for the logfiles
#       $server:        Servername used for the logfile
#       #message:       Message that need to be written
Function Write-Logfile([string]$logdir, [string]$server, [string]$message){
        $date = Get-Date -format "yyyyMMdd"
        $logfile = $logdir + "\" + "logfile" + "_" + $date + "_" + $server + ".log"
        
        Add-Content $logfile "$message"
}
 
# Loog through the servers
Foreach($server In $arrServers){
        # Create a ping object to see if the server is online
        $ping = New-Object System.Net.NetworkInformation.Ping
        $reply = $ping.send($server)
        
        # If the server gives a reply
        If($reply.status -eq "Success"){
                # Write to logfile
                Write-Logfile $logdir $server "[$server]"
                
                # Get all the folders with the servername
                $folders = $arrFolders -match $server
                
                # Loop through all the folders 
                Foreach($folder In $folders){
                        # Check if the folder exists
                        $f = $server + "\" + $subfolder + "\" + $folder
                        If(Test-Path $f){
                                # If it does exist write to logfile
                                Write-Logfile $logdir $server $folder
                                # Create the new path for the files
                                $serverArchive = $archive + "\" + $server
                                # Test if the path exists
                                If(!(Test-Path $serverArchive)){
                                        New-Item $serverArchive -Type directory
                                }
                                # Copy the items
                                robocopy $f $serverArchive
                                
                        }
                }
        }
}

Open in new window

0
 

Author Closing Comment

by:philwitkowski
ID: 31566556
sstad,
The subfolder was my issue!

Works great!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

715 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