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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

896 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

15 Experts available now in Live!

Get 1:1 Help Now