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
672 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
  • 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
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

 

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

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

785 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