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

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
philwitkowskiAsked:
Who is Participating?
 
Sander StadSysteemontwikkelaar, Database AdministratorCommented:
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
 
Sander StadSysteemontwikkelaar, Database AdministratorCommented:
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
 
philwitkowskiAuthor Commented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Sander StadSysteemontwikkelaar, Database AdministratorCommented:
My folders.txt looked like this:

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


0
 
philwitkowskiAuthor Commented:
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
 
philwitkowskiAuthor Commented:
sstad,
The subfolder was my issue!

Works great!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.