Friday, June 6, 2014

Copy files using Powershell

#=====================================================================================================#
#    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
# 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
   }
}
 
==========*=================*------------------------------*------------------
 
 
 ------------
SetFSO = CreateObject("Scripting.FileSystemObject")
strSourcePath = "C:\Data\Folder1\"
strDestinationPath = "\\Server2\d$\Folder2\"

File1 = "y-GDetail.txt"
File2 = "a-GrpHeader.txt"
File3 = "z-GrpDetail.txt"

CurrentDate = Year(Now) & Month(Now) & Day(Now)

NewFile1 = strDestinationPath & Left(File1, Len(File1)-4) & CurrentDate & Right(File1, 4)
NewFile2 = strDestinationPath & Left(File2, Len(File2)-4) & CurrentDate & Right(File2, 4)
NewFile3 = strDestinationPath & Left(File3, Len(File3)-4) & CurrentDate & Right(File3, 4)

fso.CopyFilestrSourcePath & File1, NewFile1
fso.CopyFilestrSourcePath & File2, NewFile2
fso.CopyFilestrSourcePath & File3, NewFile3

Iffso.FileExists(strDestinationPath & NewFile1) And_
    fso.FileExists(strDestinationPath & NewFile2) And_
    fso.FileExists(strDestinationPath & NewFile3) Then
    
    SetobjEmail = CreateObject("CDO.Message")
    objEmail.From = "admin1@fabrikam.com"
    objEmail.To = "admin2@fabrikam.com"
    objEmail.Subject = "3 Files Copied to Server2"
    objEmail.Textbody = "3 Files Copied to Server2 at : " & Now
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smarthost"
    objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
Else
    MsgBox"Copy Failed"
EndIf

MsgBox"Completed"