Automating FileFortress with Scripts
PowerShell and Bash scripting for automated workflows
Overview
FileFortress supports non-interactive mode for automation. This allows you to create scripts that run FileFortress commands without user interaction, perfect for scheduled tasks, backup workflows, and monitoring.
Non-Interactive Mode
Add --non-interactive flag to any command to suppress prompts and confirmations:
filefortress remotes scan --all --non-interactive
PowerShell Scripting (Windows)
Basic Scan Script
simple-scan.ps1
# Simple scan script
$ErrorActionPreference = "Stop"
# Define key file location
$keyFile = "$env:USERPROFILE\filefortress.key"
try {
Write-Host "Starting FileFortress scan..." -ForegroundColor Green
filefortress --key-file $keyFile remotes scan --all --non-interactive
Write-Host "Scan completed successfully!" -ForegroundColor Green
}
catch {
Write-Host "Scan failed: $_" -ForegroundColor Red
exit 1
}
Advanced Scan Script with Logging
advanced-scan.ps1
# Advanced scan script with logging and error handling
$ErrorActionPreference = "Stop"
# Configuration
$keyFile = "$env:USERPROFILE\filefortress.key"
$logDir = "$env:USERPROFILE\FileFortress\logs"
$logFile = Join-Path $logDir "scan-$(Get-Date -Format 'yyyy-MM-dd').log"
# Ensure log directory exists
if (-not (Test-Path $logDir)) {
New-Item -ItemType Directory -Path $logDir | Out-Null
}
function Write-Log {
param($Message, $Level = "INFO")
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"[$timestamp] [$Level] $Message" | Tee-Object -FilePath $logFile -Append
}
try {
Write-Log "Starting FileFortress scan"
# Check for updates
Write-Log "Checking for updates..."
filefortress --key-file $keyFile update --non-interactive
# Scan all remotes
Write-Log "Scanning remotes..."
filefortress --key-file $keyFile remotes scan --all --non-interactive
Write-Log "Scan completed successfully" "SUCCESS"
}
catch {
Write-Log "Scan failed: $_" "ERROR"
exit 1
}
Scheduled Scan with Task Scheduler
Create Scheduled Task (PowerShell)
# Create scheduled task for daily scan at 2 AM
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" `
-Argument "-NoProfile -ExecutionPolicy Bypass -File `"C:\Scripts\advanced-scan.ps1`""
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
$principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" `
-LogonType S4U
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -RunOnlyIfNetworkAvailable
Register-ScheduledTask -TaskName "FileFortress Daily Scan" `
-Action $action -Trigger $trigger -Principal $principal -Settings $settings
Bash Scripting (Linux/Mac)
Basic Scan Script
simple-scan.sh
#!/bin/bash
set -e
# Define key file location
KEY_FILE="$HOME/filefortress.key"
echo "Starting FileFortress scan..."
filefortress --key-file "$KEY_FILE" remotes scan --all --non-interactive
echo "Scan completed successfully!"
Make executable: chmod +x simple-scan.sh
Advanced Scan Script with Logging
advanced-scan.sh
#!/bin/bash
set -e
# Configuration
KEY_FILE="$HOME/filefortress.key"
LOG_DIR="$HOME/FileFortress/logs"
LOG_FILE="$LOG_DIR/scan-$(date +%Y-%m-%d).log"
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Logging function
log() {
local level=$1
shift
echo "[$(date '+%Y-%m-%d %H:%M:%S')] [$level] $*" | tee -a "$LOG_FILE"
}
# Main script
log INFO "Starting FileFortress scan"
# Check for updates
log INFO "Checking for updates..."
if filefortress --key-file "$KEY_FILE" update --non-interactive; then
log INFO "Update check completed"
else
log WARN "Update check failed, continuing with scan"
fi
# Scan all remotes
log INFO "Scanning remotes..."
if filefortress --key-file "$KEY_FILE" remotes scan --all --non-interactive; then
log SUCCESS "Scan completed successfully"
exit 0
else
log ERROR "Scan failed"
exit 1
fi
Scheduled Scan with Cron
Add to Crontab
# Edit crontab
crontab -e
# Add daily scan at 2 AM
0 2 * * * /home/username/scripts/advanced-scan.sh
# Or hourly scan
0 * * * * /home/username/scripts/advanced-scan.sh
# Or weekly scan (Sunday at 3 AM)
0 3 * * 0 /home/username/scripts/advanced-scan.sh
Common Automation Patterns
Scan Specific Remote
# PowerShell
filefortress --key-file $keyFile remotes scan "Google Drive" --non-interactive
# Bash
filefortress --key-file "$KEY_FILE" remotes scan "Google Drive" --non-interactive
Scan with Enrichment
# Scan and enrich (calculate hashes)
filefortress --key-file $keyFile remotes scan --all --non-interactive
filefortress --key-file $keyFile remotes enrich --all --non-interactive
Conditional Scanning
PowerShell: Only scan if network is available
if (Test-Connection -ComputerName google.com -Count 1 -Quiet) {
filefortress --key-file $keyFile remotes scan --all --non-interactive
} else {
Write-Log "Network unavailable, skipping scan" "WARN"
}
Error Handling Best Practices
PowerShell Error Handling
$ErrorActionPreference = "Stop"
try {
filefortress --key-file $keyFile remotes scan --all --non-interactive
}
catch {
Write-Log "Error: $_" "ERROR"
# Send notification (example)
Send-MailMessage -To "[email protected]" `
-Subject "FileFortress Scan Failed" `
-Body "Error: $_"
exit 1
}
Bash Error Handling
#!/bin/bash
set -e # Exit on error
# Error trap
trap 'log ERROR "Script failed at line $LINENO"' ERR
if ! filefortress --key-file "$KEY_FILE" remotes scan --all --non-interactive; then
log ERROR "Scan failed"
# Send notification
echo "FileFortress scan failed" | mail -s "Scan Failed" [email protected]
exit 1
fi
Monitoring and Alerts
Email Notifications (PowerShell)
function Send-ScanNotification {
param(@Status, @Message)
@emailParams = @{
To = "[email protected]"
From = "[email protected]"
Subject = "FileFortress Scan: @Status"
Body = @Message
SmtpServer = "smtp.example.com"
}
Send-MailMessage @@emailParams
}
try {
filefortress --key-file @keyFile remotes scan --all --non-interactive
Send-ScanNotification "Success" "Scan completed at @(Get-Date)"
}
catch {
Send-ScanNotification "Failed" "Error: @_"
}
Slack Notifications (Bash)
#!/bin/bash
SLACK_WEBHOOK="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
send_slack() {
local message=$1
curl -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"$message\"}" \
"$SLACK_WEBHOOK"
}
if filefortress --key-file "$KEY_FILE" remotes scan --all --non-interactive; then
send_slack "✅ FileFortress scan completed successfully"
else
send_slack "❌ FileFortress scan failed"
fi
Advanced Automation Examples
Backup Workflow
Automated backup verification
# PowerShell: Verify files exist in backup location
$criticalFiles = @("important.doc", "data.xlsx", "backup.zip")
foreach ($file in $criticalFiles) {
$results = filefortress --key-file $keyFile search $file --non-interactive
if ($results -match $file) {
Write-Log "✓ $file found in cloud storage" "SUCCESS"
} else {
Write-Log "✗ $file NOT found in cloud storage" "ERROR"
}
}
Duplicate Detection
Find and report duplicates
# Bash: Find duplicates and generate report
REPORT_FILE="$HOME/duplicates-$(date +%Y-%m-%d).txt"
filefortress --key-file "$KEY_FILE" find duplicates --non-interactive > "$REPORT_FILE"
if [ -s "$REPORT_FILE" ]; then
log WARN "Duplicates found, see $REPORT_FILE"
# Send report via email
mail -s "Duplicate Files Report" [email protected] < "$REPORT_FILE"
else
log INFO "No duplicates found"
fi
Best Practices
Always use
--non-interactive in automated scripts Use key files instead of passwords in scripts
Implement comprehensive logging
Add error handling and notifications
Test scripts manually before scheduling
Secure key files with proper permissions
Monitor script execution (check logs regularly)
Schedule scans during off-peak hours
Related Resources
Encryption Guide
Key file management and security
remotes Command
Scan and enrich command reference
Multi-Device Setup
Set up automation across devices