#
Copy all files in this folder structure to Egnyte.
Each folder should contain a path.txt file with the Egnyte Path where these files live.
This script will copy these files to Egnyte nightly. It will email helpdesk@mpe.ca with any folders that do NOT contain a PATH.TXT file, as well as any failures.
#>
$Local_Revit_Root = "\\mpe.ca\onprem\Revit\"
$OnPrem_DataCollector = "\\192.168.2.75\scache\Shared\"
$ErrorMessage = @()
## Email Values
$smtpserver = 'mail.mpe.ca'
$smtpport = 25
$From = "helpdesk@mpe.ca"
$send_to = "eeckert@mpe.ca"
$subject_line = "Errors: Revit to Egnyte Sync"
function Send-Email {
# Basic Send Email function
param (
[Parameter(Mandatory = $true)]
[string]$To,
[Parameter(Mandatory = $true)]
[string]$Subject,
[Parameter(Mandatory = $true)]
[string]$Body,
[Parameter(Mandatory = $false)]
[string[]]$AttachmentPaths
)
# Create a new MailMessage object
$MailMessage = New-Object System.Net.Mail.MailMessage
$MailMessage.From = $From
$MailMessage.To.Add($To)
$MailMessage.Subject = $Subject
$MailMessage.Body = $Body
$MailMessage.IsBodyHtml = $true
# Add attachments to the MailMessage
foreach ($AttachmentPath in $AttachmentPaths) {
if (Test-Path $AttachmentPath) {
$Attachment = New-Object System.Net.Mail.Attachment($AttachmentPath)
$MailMessage.Attachments.Add($Attachment)
}
else {
Write-Host "Attachment not found: $AttachmentPath"
}
}
# Configure SMTP client
$SMTPClient = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
# $SMTPClient.EnableSsl = $true
# $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
# Send the email
try {
$SMTPClient.Send($MailMessage)
Write-Host "Email sent successfully to $To"
}
catch {
Write-Host "Error sending email: $_"
}
}
$Local_Folder_List = get-childitem -path $Local_Revit_Root -Depth 0
foreach ($folder in $Local_Folder_List) {
if ($folder.Name -eq "!TEMPLATE") {
# Do nothing
} elseif ($folder.Name -like "*.lnk") {
# Do Nothing
}
else {
$FolderName = $folder.Name
$path_to_pathtxt = $folder.FullName + "\path.txt"
if ((get-content $path_to_pathtxt) -eq "IgnoreMe") {
# allows us to set folders to ignored by changing Path.txt to contain IgnoreMe
# Break leaves the loop and goes to the next iteration
break
}
$PAthTXT_Exists = Test-Path $path_to_pathtxt
if ($PAthTXT_Exists) {
$NumberofLines = get-content $path_to_pathtxt | measure -Line
$Egnyte_Path = get-content $path_to_pathtxt
$OnPrem_DC_Path = $Egnyte_Path.Replace("M:\", $OnPrem_DataCollector)
$OnPrem_DataCollector_up = Test-Path $OnPrem_DC_Path
if ($OnPrem_DataCollector_up) {
# Validate Egnyte Path
if (!(Test-Path $OnPrem_DC_Path)) {
$Egnyte_Path_Valid = $false
$ErrorMessage += '`"' + $FolderName + "`": Path.txt error: Egnyte Path Doesn't Exist.
"
}
else {
$Egnyte_Path_Valid = $true
}
if (!($NumberofLines.Lines -eq 1)) {
$Egnyte_Path_Valid = $false
$ErrorMessage += '`"' + $FolderName + "`": Path.txt must specify only one path.
"
}
else {
$Egnyte_Path_Valid = $true
}
# copy files to Egnyte
if ($Egnyte_Path_Valid) {
Write-Output "Copying $foldername to Egnyte"
# $Source_Path = $folder.FullName + "\*"
$Source_Path = $folder.FullName
$LogPath = $OnPrem_DC_Path + '\CopyLog.log'
robocopy.exe $Source_Path $OnPrem_DC_Path /MT:5 /w:5 /r:5 /XO /COPY:DAT /log:$LogPath
# Copy-Item $Source_Path $OnPrem_DC_Path -Force -Recurse
}
} else {
$ErrorMessage += '`"' + $FolderName + "`": On Premise Path not Valid: `"$OnPrem_DC_Path`". Is the server up?
"
}
}
else {
$ErrorMessage += $FolderName + ": Path.txt error: Path.txt doesn't exist.
"
}
}
}
if ($ErrorMessage) {
#If we have errors, send an email to be handled.
$email_body = "