IT Pros – The KB Location

Windows Power Shell File Moves and Alerts

Windows Power Shell File Moves and Alerts

I came across a need to setup a script to move CSV files from a production machine, to a SQL server for some reporting that was needed. I addressed the immediate need with a basic batch file that made the move. However, outside of verifying the file existed in the destination, and/or looking at the scheduled tasks to verify it ran, there was no solid method of verification of execution.

Using various scripts and snippets I found online, I pulled together the script below.

Note: This only works for same domain (or in this case 1 domain with a NON-DOMAIN Windows machine). Some functions have been of course, replaced with generic information to avoid exposing usernames and email addresses.

 

# 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 = “emailaddress@domain.com” # Enter the TO address for the e-mail alert
$smtpsrv = “SMTP.SERVER.HERE” # Enter the FQDN or IP of a SMTP relay

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive(“Z:”, “\\hostname\Share”, $false, “domain\user”, “passwordhere”)

# 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
# Used Z: here as noted in the above mapnetwork drive object as the source is non-domain and needed auth to connect
$Localfiles = “Z:\*.*”
# Remote Path should be UNC path with admin share example \\hostname\c$\pathtocopyto
$Remotefiles = “\\destinationhost\Share”

#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 = $FALSE # 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 sendto@email.com -from from@email.com -Subject “MACHINENAME Files have been copied” -body “The following files were moved: $Files ” -SmtpServer smtpname.here.com
}

}

Exit mobile version