AJJaxNet

Syncro Force Full Sync

Mar 12th, 2021 (edited)
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .Synopsis
  3.   Forces the Syncro agent to perform a Full Sync.
  4.  
  5. .DESCRIPTION
  6.   Take note that this script will restart the Syncro service. This is done on a delay so the script is reported as successful on the dashboard.
  7.  
  8.   The script also adds an Activity Log on the asset if the full sync was successful.
  9.  
  10.   USE AT YOUR OWN RISK.
  11.  
  12. .NOTES
  13.   Version:        1.0
  14.   Author:         Alexandre-Jacques St-Jacques
  15.   Creation Date:  15-05-2021
  16.   Purpose/Change: Initial script development
  17.  
  18. .LINK
  19.   https://github.com/Maelstrom96/SyncroMSP-Scripts/blob/main/scripts/syncro-force-fullsync.ps1
  20. #>
  21.  
  22. $UpdateTime = (Get-Date).ToUniversalTime().AddMinutes(5).ToString("yyyy-MM-ddTHH:mm:ss.0000000Z")
  23. #Update Syncro last_sync registry value
  24. Set-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\RepairTech\Syncro" -Name "last_sync" -Value "$UpdateTime"
  25.  
  26. function Run-InNewProcess{
  27.   param([String] $code)
  28.   $code = "function Run{ $code }; Run $args"
  29.   $encoded = [Convert]::ToBase64String( [Text.Encoding]::Unicode.GetBytes($code))
  30.  
  31.   start-process -WindowStyle hidden PowerShell.exe -argumentlist '-windowstyle','hidden','-noExit','-encodedCommand',$encoded
  32. }
  33.  
  34. $script = {
  35.     $CurrentDateString = (Get-Date).ToString("yyyyMMdd")
  36.     $LogLocation = "C:\ProgramData\Syncro\logs\$CurrentDateString-Syncro.Service.Runner.log"
  37.    
  38.     try {
  39.         Import-Module $env:SyncroModule -erroraction stop
  40.     }
  41.     catch {
  42.         $env:RepairTechUUID = (Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\RepairTech\Syncro" -Name "uuid").uuid
  43.         $env:RepairTechApiBaseURL = "syncromsp.com"
  44.         $env:RepairTechApiSubDomain = (Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\RepairTech\Syncro" -Name "shop_subdomain").shop_subdomain
  45.         $env:RepairTechFilePusherPath = "$($env:PROGRAMDATA)\Syncro\bin\FilePusher.exe"
  46.  
  47.         Import-Module "$($env:PROGRAMDATA)\Syncro\bin\module.psm1" 3>$null
  48.     }
  49.    
  50.     Start-Sleep -s 10;
  51.     Restart-Service -Name "Syncro" -Force
  52.    
  53.     Log-Activity -Message "Restarted Syncro Service for Full Sync" -EventName "SyncroRestart"
  54.  
  55.     # Hack to get Get-Content -wait to work properly
  56.     $hackJob = Start-Job {
  57.       $f=Get-Item $LogLocation
  58.       while (1) {
  59.         $f.LastWriteTime = Get-Date
  60.         Start-Sleep -Seconds 1
  61.       }
  62.     }
  63.    
  64.     # Job that confirms if the sync happened
  65.     $job = Start-Job { param($LogLocation)
  66.             try {
  67.                 Import-Module $env:SyncroModule -erroraction stop
  68.             }
  69.             catch {
  70.                 $env:RepairTechUUID = (Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\RepairTech\Syncro" -Name "uuid").uuid
  71.                 $env:RepairTechApiBaseURL = "syncromsp.com"
  72.                 $env:RepairTechApiSubDomain = (Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\RepairTech\Syncro" -Name "shop_subdomain").shop_subdomain
  73.                 $env:RepairTechFilePusherPath = "$($env:PROGRAMDATA)\Syncro\bin\FilePusher.exe"
  74.  
  75.                 Import-Module "$($env:PROGRAMDATA)\Syncro\bin\module.psm1" 3>$null
  76.             }
  77.        
  78.         Get-Content $LogLocation -tail 0 -wait | where { $_ -match "Large sync complete" } |% { Log-Activity -Message "Full Sync Successful" -EventName "SyncroFullSync"; break }
  79.     } -Arg $LogLocation
  80.    
  81.     # Wait for the Activity-Log job to complete or to timeout
  82.     Wait-Job $job -Timeout 60
  83.    
  84.     # Cleanup jobs
  85.     Get-Job | Stop-Job
  86.     Get-Job | Remove-Job
  87. }
  88.  
  89. Run-InNewProcess $script | Out-Null
  90.  
  91. Exit 0
Add Comment
Please, Sign In to add comment