Enjoy!
Code: Select all
##############################################################################################################
# #
# Name: gw_filezilla_settings_sync #
# Purpose: Copies FileZilla settings from one GW server to the other, if changed #
# Created By: MJ 05/13/2016 #
# #
#Script makes use of: https://gallery.technet.microsoft.com/scriptcenter/Powershell-FileSystemWatche-dfd7084b#
# #
# #
##############################################################################################################
#Kill all other instances of script
$powershellKill = Get-WmiObject Win32_Process -Filter "Name='powershell.exe' AND NOT ProcessId LIKE '$pid'"
$powershellKill = $powershellKill.ProcessID
$powershellKill
foreach ($procid in $powershellKill) {
Stop-Process $procid -Force
}
Write-Output "Starting" | Out-File -FilePath C:\DevOps\Logs\gw_filezilla_settings_sync.txt
# To stop the monitoring, run the following commands:
$event = Get-EventSubscriber
$event = $event.SourceIdentifier
if ($event -eq "Filezilla_Changed") {
Unregister-Event Filezilla_Changed
}
$localFolder = 'C:\Program Files (x86)\FileZilla Server' # Enter the root path you want to monitor.
$filter = '*.xml' # You can enter a wildcard filter here.
# In the following line, you can change 'IncludeSubdirectories to $true if required.
$fsw = New-Object System.IO.FileSystemWatcher $localFolder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
Register-ObjectEvent $fsw Changed -SourceIdentifier Filezilla_Changed -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
$hostName = $env:computername
if ($hostName -eq "Server01") {
$remoteHost = "Server02"
}
if ($hostName -eq "Server02") {
$remoteHost = "Server01"
}
$localFolder = 'C:\Program Files (x86)\FileZilla Server'
$remoteFolder = "\\$remoteHost\C$\Program Files (x86)\FileZilla Server"
Copy-Item "$localFolder\$name" "$remoteFolder\$name" -Force
#Invoke-Command
Invoke-Command -ComputerName $remoteHost -ScriptBlock { & cmd.exe /c '"C:\Program Files (x86)\FileZilla Server\FileZilla Server.exe" /reload-config' }
Out-File -FilePath C:\DevOps\Logs\gw_filezilla_settings_sync.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"}