Last active
June 16, 2021 06:15
-
-
Save mnadjit/dd8bca21daf98badeaec6d9ad4ef311d to your computer and use it in GitHub Desktop.
This script moves folders with any date timestamp except for today, and moves it to a remote UNC path. Also checks free disk space and sends out notification email if under threshold - also checks for a file with fail*.* format that indicates another process failed to run properly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CreateFailureFlagFile { | |
$script:FailureFilePath = "$(Get-Location)\\_Failure-$(Get-Date -format yyyyMMdd).txt" | |
ECHO 'Issue occured during processing of MoveFolders-EmailLowDiskFreeSpace.ps1 - Please refer to log file in same directory named TransferLog.txt' | Out-File -FilePath $FailureFilePath | |
} | |
function GetConfigs { | |
try { | |
$script:LogPath = Join-Path -Path $(Get-Location).Path -ChildPath "TransferLog.txt" | |
MoveLogToOldLogIfLarge | |
ECHO "`r`n" >> $LogPath | |
ECHO "----------------------------------------------------------" >> $LogPath | |
ECHO "Start: $(Get-Date -Format "yyyy-MM-dd HH:mm")" >> $LogPath | |
$configJson = Get-Content '.\config.json' | Out-String | ConvertFrom-Json | |
$script:DriveToCheckSpace = $configJson.TransferMonitor.DriveToCheckSpace | |
$script:FreeSpaceThreshold = $configJson.TransferMonitor.FreeSpaceThreshold | |
$script:FreeSpacePercThreshold = $configJson.TransferMonitor.FreeSpacePercThreshold | |
$script:FailureFilePrefix = $configJson.TransferMonitor.FailureFilePrefix | |
$script:SmtpServer = $configJson.Email.SmtpServer | |
$script:EmailSender = $configJson.Email.EmailSender | |
$script:EmailRecipients = $configJson.Email.EmailRecipients | |
$script:EmailSubject = $configJson.Email.EmailSubject | |
$script:EmailPriority = $configJson.Email.EmailPriority | |
$script:FailureDetectedEmailBodyRaw = Get-Content -Raw $configJson.Email.FailureDetectedEmailBody | |
$script:DiskSizeIssueEmailBodyRaw = Get-Content -Raw $configJson.Email.DiskSizeIssueEmailBody | |
$script:SourceFolderPath = $configJson.MasterLogTransfer.SourceFolderPath | |
$script:DestinationUNCPath = $configJson.MasterLogTransfer.DestinationUNCPath | |
ECHO 'Successfully retrieved configuration parameters from config.json file.' >> $LogPath | |
} | |
catch { | |
ECHO 'Failed to retrieve configuration parameters from config.json file.' >> $LogPath | |
ECHO $_ >> $LogPath | |
EXIT | |
} | |
} | |
function NotifyIfAnyIssues { | |
$Disks=Get-CimInstance -Class CIM_LogicalDisk | Select-Object @{Name="SizeGB";Expression={[math]::Round($_.size/1gb)}}, @{Name="FreeSpace";Expression={[math]::Round($_.freespace/1gb)}}, @{Name="FreePerc";Expression={[math]::Round(($_.freespace/1gb)*100 / ($_.size/1gb))}}, DeviceID, DriveType | Where-Object DriveType -EQ '3' | |
$DiskToCheck=$Disks | Where-Object { $_.DeviceID -eq $DriveToCheckSpace} | |
$FreeGB = $DiskToCheck.FreeSpace | |
$FreePerc = $DiskToCheck.FreePerc | |
$DiskSize = $DiskToCheck.SizeGB | |
$emailParams = @{ | |
SmtpServer = $SmtpServer | |
To = $EmailRecipients | |
From = $EmailSender | |
Priority = $EmailPriority | |
Subject = $EmailSubject | |
} | |
if ($(Get-ChildItem | Where {$_.Name.Contains("_Failure") -And $_.CreationTime -lt (Get-Date).AddMinutes(-5) }).Count -gt 0) { | |
ECHO '*** FAILURE *** Failure flag file detected in the folder where this process is running. Something has gone wrong in previous instances of this process.' >> $LogPath | |
$FailureDetectedEmailBody = $ExecutionContext.InvokeCommand.ExpandString($FailureDetectedEmailBodyRaw) | Out-String | |
Send-MailMessage @emailParams -Body $FailureDetectedEmailBody -BodyAsHtml | |
} | |
if ($FreeGB -lt $FreeSpaceThreshold -Or $FreePerc -lt $FreeSpacePercThreshold) { | |
ECHO '*** ISSUE *** Disk free space is lower than thresholds set in the config file.' >> $LogPath | |
$DiskSizeIssueEmailBody = $ExecutionContext.InvokeCommand.ExpandString($DiskSizeIssueEmailBodyRaw) | Out-String | |
Send-MailMessage @emailParams -Body $DiskSizeIssueEmailBody -BodyAsHtml | |
} | |
} | |
function MoveFoldersExceptTodayFolder { | |
$OldFolder = dir $SourceFolderPath -Recurse -Depth 2 -Directory -Exclude .temp | Where { $_.Name.Length -eq 8 -AND $_.Name -ne $(Get-Date -Format yyyyMMdd) } | |
$OldFolder | ForEach-Object -Process { | |
$newFolderPath = Join-Path $DestinationUNCPath -ChildPath $_.FullName.Replace($SourceFolderPath,"") | |
try { | |
$OldFolderCount = $(Get-ChildItem $_).Count | |
MOVE -Force $_ $newFolderPath | |
Get-Job | Wait-Job | |
$NewFolderCount = $(Get-ChildItem $newFolderPath).Count | |
if ($OldFolderCount -ne $NewFolderCount) { | |
ECHO "*** FAILURE *** Number of files in the old folder [$_] do not match the destination folder [$newFolderPath]" >> $LogPath | |
EXIT | |
} | |
ECHO "*** SUCCESS *** Successfully moved [$_] folder to destination folder [$newFolderPath]" >> $LogPath | |
RM $FailureFilePath | |
} catch { | |
ECHO "*** FAILURE *** Failed to move [$_] folder to destination folder [$newFolderPath]" >> $LogPath | |
} | |
} | |
} | |
function MoveLogToOldLogIfLarge { | |
if (-Not (Test-Path -Path $LogPath -PathType leaf)) { return }; | |
$LogSize = $(Get-Item $LogPath).Length | |
if ($LogSize -gt 500000) { | |
MOVE -Force $LogPath "$($LogPath.substring(0, $LogPath.length - 4)).old.txt" | |
} | |
} | |
CreateFailureFlagFile | |
GetConfigs | |
NotifyIfAnyIssues | |
MoveFoldersExceptTodayFolder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"MasterLogTransfer": { | |
"SourceFolderPath": "C:\\Local-Folder", | |
"DestinationUNCPath": "\\\\backup-server\folder", | |
"LogFileName": "ProcessLog.txt" | |
}, | |
"TransferMonitor": { | |
"DriveToCheckSpace": "L:", | |
"FreeSpaceThreshold": 5, | |
"FreeSpacePercThreshold": 25, | |
"FailureFilePrefix": "_fail" | |
}, | |
"Email": { | |
"SmtpServer": "IP or Hostname", | |
"EmailSender": "[email protected]", | |
"EmailRecipients": "Name Surname <[email protected]>", | |
"EmailSubject": "Email Subject", | |
"EmailPriority": "High", | |
"FailureDetectedEmailBody": "Failure-Detected-EmailTemplate.html", | |
"DiskSizeIssueEmailBody": "DiskSize-EmailTemplate.html" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body { | |
font-family: Segoe UI Light, Tahoma, Arial, sans-serif; | |
} | |
table { | |
background-color: #EEE; | |
border-collapse: collapse; | |
width: 330px; | |
} | |
td { | |
width: 33%; | |
border: 1px solid #DDD; | |
text-align: center; | |
} | |
th { | |
background-color: #FFF; | |
} | |
code { | |
font-family: Consolas; | |
font-size: 110% | |
} | |
snap { | |
font-family: courier-new; | |
} | |
</style> | |
</head> | |
<body> | |
<title>Rhapsody Master Log - Drive Space</title> | |
<h2>Free space needs to be checked for Rhapsody Master Logs</h2> | |
<h4>Please check $DriveToCheckSpace on <code>NHRhapsodyProd.nh.org.au</code></h4> | |
<table> | |
<tr> | |
<th></th> | |
<th></th> | |
<th>Threshold</th> | |
</tr> | |
<tr> | |
<td>Drive</td> | |
<td>$DriveToCheckSpace</td> | |
<td></td> | |
</tr> | |
<tr> | |
<td>Drive Size</td> | |
<td>$DiskSize</td> | |
<td></td> | |
</tr> | |
<tr> | |
<td>Free Size (GB)</td> | |
<td>$FreeGB</td> | |
<td>$FreeSpaceThreshold</td> | |
</tr> | |
<tr> | |
<td>Free Size (%)</td> | |
<td>$FreePerc %</td> | |
<td>$FreeSpacePercThreshold %</td> | |
</tr> | |
</table> | |
<br/><br/> | |
<h4>Please check the log in the following path on <code>NHRhapsodyProd.nh.org.au</code></h4> | |
<snap>L:\Rhapsody Logs\MasterLog-Transfer\Processlog.txt</snap><br/><br/> | |
<hr> | |
<snap>Documentation available at: <a href="https://ictwiki.nh.org.au/index.php/Rhapsody_Master_Logs_move_to_the_Backup_Server">https://ictwiki.nh.org.au/index.php/Rhapsody_Master_Logs_move_to_the_Backup_Server</a></snap> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body { | |
font-family: Segoe UI Light, Tahoma, Arial, sans-serif; | |
} | |
code { | |
font-family: Consolas; | |
font-size: 110% | |
} | |
snap { | |
font-family: courier-new; | |
} | |
</style> | |
</head> | |
<body> | |
<title>Rhapsody Master Log Move Failure!</title> | |
<h2>Rhapsody Master Log Move Failure!</h2> | |
<h4>Please check the log in the following path on <code>NHRhapsodyProd.nh.org.au</code></h4> | |
<snap>L:\Rhapsody Logs\MasterLog-Transfer\Processlog.txt</snap><br/><br/> | |
<hr> | |
<snap>Documentation available at: <a href="https://ictwiki.nh.org.au/index.php/Rhapsody_Master_Logs_move_to_the_Backup_Server">https://ictwiki.nh.org.au/index.php/Rhapsody_Master_Logs_move_to_the_Backup_Server</a></snap> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment