Created
May 26, 2023 17:38
-
-
Save EndPointers/f06dad20ed145de5ec25c7b30c63061e to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.ServiceProcess; | |
using System.Text; | |
using System.Threading; | |
using Microsoft.Win32; | |
namespace upgrader | |
{ | |
internal class Program | |
{ | |
[DllImport("user32.dll")] | |
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); | |
[DllImport("user32.dll")] | |
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); | |
private static string hostName; | |
private static string joinedDomain; | |
private static void Log(string msgState, string logMessage) | |
{ | |
using (StreamWriter w = File.AppendText("C:\\upgrader.log")) | |
{ | |
w.WriteLine($"{msgState}: {logMessage} - {DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}"); | |
} | |
} | |
private static void StartProcess(string cmd, string args) | |
{ | |
try | |
{ | |
Process process = new Process(); | |
process.StartInfo.FileName = cmd; | |
process.StartInfo.Arguments = args; | |
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; | |
process.Start(); | |
process.WaitForExit(); | |
} | |
catch (Exception ex) | |
{ | |
Log("ERROR", ex.Message); | |
} | |
} | |
private static void StopService(string name) | |
{ | |
try | |
{ | |
ServiceController service = new ServiceController(name); | |
service.Stop(); | |
} | |
catch (Exception ex) | |
{ | |
Log("ERROR", ex.Message); | |
} | |
} | |
private static void StopProcess(string name) | |
{ | |
try | |
{ | |
Process[] workers = Process.GetProcessesByName(name); | |
foreach (Process worker in workers) | |
{ | |
worker.Kill(); | |
worker.WaitForExit(); | |
worker.Dispose(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Log("ERROR", ex.Message); | |
} | |
} | |
public static string SearchSubKeys(RegistryKey root, String skValue, String search) | |
{ | |
string result = null; | |
foreach (string keyname in root.GetSubKeyNames()) | |
{ | |
try | |
{ | |
using (RegistryKey key = root.OpenSubKey(keyname)) | |
{ | |
foreach (string subKeyValue in key.GetValueNames()) | |
{ | |
if (subKeyValue == skValue) | |
{ | |
string val = key.GetValue(skValue).ToString(); | |
if (val == search) | |
{ | |
result = keyname; | |
goto Bail; | |
} | |
} | |
} | |
SearchSubKeys(key, skValue, search); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Log("ERROR", ex.Message); | |
} | |
} | |
Bail: | |
return result; | |
} | |
private static void RemoveItem(RegistryKey registryPath, string keyToDelete) | |
{ | |
try | |
{ | |
registryPath.DeleteSubKeyTree(keyToDelete); | |
} | |
catch (Exception ex) | |
{ | |
Log("ERROR", ex.Message); | |
} | |
} | |
private static void UninstallTeamViewer() | |
{ | |
StopProcess("TeamViewer"); | |
StopService("TeamViewer"); | |
StartProcess("C:\\Program Files (x86)\\TeamViewer\\TeamViewer_Service.exe", "-uninstall"); | |
StartProcess("C:\\Program Files (x86)\\TeamViewer\\TeamViewer.exe", "api --uninstall"); | |
StartProcess("C:\\Program Files (x86)\\TeamViewer\\tv_x64.exe", "--action removeprint --event Local\\DriverInstallFinishEvent_Printer --id \"TeamViewer Printer\""); | |
StartProcess("C:\\Program Files (x86)\\TeamViewer\\tv_x64.exe", "--action remove --id TEAMVIEWERVPN"); | |
RegistryKey SubKey = Registry.LocalMachine; | |
SubKey = SubKey.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", true); | |
string pCode = SearchSubKeys(SubKey, "DisplayName", "TeamViewer Host"); | |
if (!String.IsNullOrEmpty(pCode)) | |
{ | |
var TVUninstall = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", true); | |
RemoveItem(TVUninstall, pCode); | |
} | |
var TVURL = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes", true); | |
RemoveItem(TVURL, "teamviewer8"); | |
var TVSOFT = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node", true); | |
RemoveItem(TVSOFT, "TeamViewer"); | |
RegistryKey SubKey2 = Registry.ClassesRoot; | |
SubKey2 = SubKey2.OpenSubKey(@"Installer\Products", true); | |
string pCode2 = SearchSubKeys(SubKey2, "ProductName", "TeamViewer Host"); | |
if (!String.IsNullOrEmpty(pCode2)) | |
{ | |
var TVUninstall2 = Registry.ClassesRoot.OpenSubKey(@"Installer\Products", true); | |
RemoveItem(TVUninstall2, pCode2); | |
} | |
RegistryKey SubKey3 = Registry.LocalMachine; | |
SubKey3 = SubKey3.OpenSubKey(@"SOFTWARE\Classes\Installer\Products", true); | |
string pCode3 = SearchSubKeys(SubKey3, "ProductName", "TeamViewer Host"); | |
if (!String.IsNullOrEmpty(pCode3)) | |
{ | |
var TVUninstall3 = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Installer\Products", true); | |
RemoveItem(TVUninstall3, pCode3); | |
} | |
try | |
{ | |
Directory.Delete(@"C:\Program Files (x86)\TeamViewer", true); | |
} | |
catch (Exception ex) | |
{ | |
Log("ERROR", ex.Message); | |
} | |
File.Delete(@"C:\Users\Public\Desktop\TeamViewer.lnk"); | |
File.Delete(@"C:\Users\Public\Desktop\TeamViewer Host.lnk"); | |
} | |
private static void InstallUpdate() | |
{ | |
StartProcess(System.Environment.CurrentDirectory + "\\Teamviewer_Host.exe", "/S /norestart"); | |
} | |
private static void AddDomain(string host, string domain) | |
{ | |
StartProcess("C:\\Program Files (X86)\\TeamViewer\\TeamViewer.exe", "assign --api-token {API_TOKEN} --group-id {GROUP_ID} --grant-easy-access --reassign --alias \"" + host + " (" + domain + ")\""); | |
} | |
private static void RemoveDeviceFromTVList(string host) | |
{ | |
StartProcess("powershell.exe", "-ExecutionPolicy Bypass -NonInteractive -WindowStyle Hidden -File " + System.Environment.CurrentDirectory + "\\Remove-DeviceFromTVList.ps1 -alias " + host); | |
} | |
private static void HideConsole(string title) | |
{ | |
IntPtr hWnd = FindWindow(null, title); | |
if (hWnd != IntPtr.Zero) | |
{ | |
ShowWindow(hWnd, 0); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
hostName = System.Environment.MachineName; | |
joinedDomain = System.Environment.UserDomainName; | |
HideConsole(Console.Title); | |
RemoveDeviceFromTVList(hostName); | |
UninstallTeamViewer(); | |
InstallUpdate(); | |
Thread.Sleep(10000); | |
AddDomain(hostName, joinedDomain); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment