From 1f2722fa8ab23bc0f7781eaacb01fc741bb26c03 Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 24 Jun 2024 08:17:43 -0600 Subject: [PATCH] first commit --- Copy-Revit2Egnyte.ps1 | 136 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 Copy-Revit2Egnyte.ps1 diff --git a/Copy-Revit2Egnyte.ps1 b/Copy-Revit2Egnyte.ps1 new file mode 100644 index 0000000..1490999 --- /dev/null +++ b/Copy-Revit2Egnyte.ps1 @@ -0,0 +1,136 @@ +<# +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 = "R:\" +$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 -Directory + +foreach ($folder in $Local_Folder_List) { + if ($folder.Name -eq "!TEMPLATE") { + # Do nothing + } + else { + $FolderName = $folder.Name + $path_to_pathtxt = $folder.FullName + "\path.txt" + $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 $Egnyte_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 + robocopy.exe $Source_Path $OnPrem_DC_Path /MT:5 /w:5 /r:5 /XO /COPY:DAT + # 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 = "

Errors reported during the Revit Sync Process



$ErrorMessage" + Send-Email -To $send_to -Subject $subject_line -Body $email_body + +}