Powershell Script to Copy Files from One Server to Another in the same Domain.

Purpose:
Simple script to copy files from one computer or server to another in the same domain. Example can
also email a simple report of files copied. Created to move some files hourly and only email when
something was moved.

Notes:
Will not work across different domains. Account used to run task from task manager must be a domain
user with enough rights on both systems to read and write.

#=====================================================================================================#
#    Copyright 2014 Robert Stacks
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see .
#
#=====================================================================================================#
# Original Author: Robert Stacks
# Original Script URL: https://randomtechminutia.wordpress.com/2014/02/03/copy-files-from-one-server-to-another-in-the-same-domain/
# Date: 2/3/2014
# Updated: 
# Version: 1.0
#
# Purpose:
# Simple script to copy files from one computer or server to another in the same domain.  Example can 
# also email a simple report of files copied.  Created to move some files hourly and only email when 
# something was moved.
# 
# Many of the user variables are set with $TRUE or $FALSE for on or off.
#
# Will not work across different domains.  Account used to run task from task manager must be a domain 
# user with enough rights on both systems to read and write.
#
# Update Notes:
# 
#=====================================================================================================#

# Email Settings
$fromAddr = "From@domain.com" # Enter the FROM address for the e-mail alert
$toAddr = "To@domain.com" # Enter the TO address for the e-mail alert
$smtpsrv = "mail.domain.com" # Enter the FQDN or IP of a SMTP relay

# Files to Copy and where they are going
# Local Files should be in the formate of C:\Folder\* for Files only or C:\Folder to copy the folder as well
$Localfiles = "C:\Folder\*"
# Remote Path should be UNC path with admin share example \\hostname\c$\pathtocopyto
$Remotefiles = "\\hostname\c$\pathtocopyto"

#Enable or Disable Script functions
$EmailAlerts = $TRUE # Turn e-mail alerts on or off. $FALSE or 0 = off
$TestfromPrompt = $FALSE # Turn on output from command line.  $FALSE or 0 = no output
$RemoveFilesafterCopy = $TRUE # Remove files after files have been copied over?

#===========================#
#Main Script                #
#===========================#

#Get the names of the files we want to transfer
$files = Get-childitem "$Localfiles" |foreach { $_.Name}

if ($files -ne $null)
{

   #Generates output to command line if Value = True
   if ($TestfromPrompt -eq $TRUE)
   {
	Write-Host "These are the files being copied" 
	$files
   }

#Copy files to remote computer
copy-item -path "$Localfiles" -Destination "$Remotefiles" -Recurse

   #Remove Files if Value = True
   if ($RemoveFilesafterCopy -eq $TRUE)
   {
	#Remove Files after they have been moved
	Remove-Item "$Localfiles"
   }
   
   #Send Email Alert if Value = True
   if ($EmailAlerts -eq $TRUE)
   {
    $date = Get-Date -Format g
    Send-MailMessage -To $toAddr -From $fromAddr -Subject "$date Files Copied" -Body "The Following Files were moved: " + $files -SmtpServer $smtpsrv 
   }
}
Advertisements
  1. #1 by Helao on September 5, 2014 - 10:12 am

    How can i append the date and time to the destination file?

    • #2 by Robert on September 12, 2014 - 9:53 am

      Hrm I had a suggestion that was simple but the more I thought about it the more I realized it would be a bad idea. What we’ll need to do is change line 72 so its a foreach ($file in $files) loop and then as it copies each file append the date. Let me do a little code testing and I’ll post a solution.

      • #3 by Robert on September 12, 2014 - 10:42 am

        Ok I think this will do the trick, remove line 72. Add the following two lines in its place:

        $date = get-date -F MM-dd-yyyy
        Get-ChildItem -path "$Localfiles" | %{Copy_Item -path $_.FullName -Destination ($Remotefiles+"\"+$date+"-"+$_.Name)}
        

        Let me know if it works for you. Some simple testing on my part seems to work just ask expected.

    • #4 by Robert on September 12, 2014 - 10:46 am

      Oops just realized you wanted the time too so change the date line to this:
      $date = get-date -F MM-dd-yyyy-hh:mm:sstt

      You should get some output from all of this like 09-12-2014-10:01:01am-filename.ext Hopefully that works for you.
      -Robert

  2. #5 by Smash on September 12, 2014 - 6:09 am

    nice one.. was really helpful.. there is an error in the #85

    “The Following Files were moved” + $files

    it should be “The Following Files were moved $files”

    no need to add the + symbol as you can add the variable directly in the string

    • #6 by Robert on September 12, 2014 - 8:34 am

      I don’t know that is a error as such, either syntax style there should work should it not? I think you are talking more about coding preferences.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: