|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | +) |
| 7 | + |
| 8 | +func installApplications(config *Config) { |
| 9 | + logger := logMultiWriter(config.General.LogFile) |
| 10 | + |
| 11 | + // Check and install Chocolatey if not installed |
| 12 | + err := checkAndInstallChocolatey(config) |
| 13 | + if err != nil { |
| 14 | + logger.Printf("Failed to install Chocolatey: %v", err) |
| 15 | + return |
| 16 | + } |
| 17 | + |
| 18 | + apps := selectRandomOrHardcoded(config.Applications.Options, config.Applications.SelectionMethod) |
| 19 | + for _, app := range apps { |
| 20 | + cmd := exec.Command("choco", "install", app, "-y") |
| 21 | + err := cmd.Run() |
| 22 | + if err != nil { |
| 23 | + logger.Printf("Failed to install %s: %v", app, err) |
| 24 | + } else { |
| 25 | + logger.Printf("Installed %s", app) |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func manageSoftware(config *Config) { |
| 31 | + logger := logMultiWriter(config.General.LogFile) |
| 32 | + |
| 33 | + operations := selectRandomOrHardcoded(config.SoftwareManagement.Options, config.SoftwareManagement.SelectionMethod) |
| 34 | + for _, operation := range operations { |
| 35 | + cmd := exec.Command("choco", operation, "-y") |
| 36 | + err := cmd.Run() |
| 37 | + if err != nil { |
| 38 | + logger.Printf("Failed to perform software management operation %s: %v", operation, err) |
| 39 | + } else { |
| 40 | + logger.Printf("Performed software management operation %s", operation) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func addStartMenuItems(config *Config) { |
| 46 | + logger := logMultiWriter(config.General.LogFile) |
| 47 | + |
| 48 | + items := selectRandomOrHardcoded(config.StartMenuItems.Options, config.StartMenuItems.SelectionMethod) |
| 49 | + for _, item := range items { |
| 50 | + cmd := exec.Command("powershell", "-Command", fmt.Sprintf(`$s = New-Object -ComObject WScript.Shell; $shortcut = $s.CreateShortcut("C:\\Users\\Public\\Desktop\\%s.lnk"); $shortcut.TargetPath = "%s"; $shortcut.Save()`, item, item)) |
| 51 | + err := cmd.Run() |
| 52 | + if err != nil { |
| 53 | + logger.Printf("Failed to add start menu item %s: %v", item, err) |
| 54 | + } else { |
| 55 | + logger.Printf("Added start menu item %s", item) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func checkAndInstallChocolatey(config *Config) error { |
| 61 | + logger := logMultiWriter(config.General.LogFile) |
| 62 | + cmd := exec.Command("choco", "-v") |
| 63 | + err := cmd.Run() |
| 64 | + if err != nil { |
| 65 | + logger.Println("Chocolatey not found, installing...") |
| 66 | + cmd = exec.Command("powershell", "-Command", "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))") |
| 67 | + err = cmd.Run() |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to install Chocolatey: %v", err) |
| 70 | + } |
| 71 | + logger.Println("Chocolatey installed successfully") |
| 72 | + } |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func checkAndInstallPSWindowsUpdate(config *Config) error { |
| 77 | + logger := logMultiWriter(config.General.LogFile) |
| 78 | + cmd := exec.Command("powershell", "-Command", "Get-Module -ListAvailable -Name PSWindowsUpdate") |
| 79 | + err := cmd.Run() |
| 80 | + if err != nil { |
| 81 | + logger.Println("PSWindowsUpdate module not found, installing...") |
| 82 | + cmd = exec.Command("powershell", "-Command", "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-Module -Name PSWindowsUpdate -Force") |
| 83 | + err = cmd.Run() |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("failed to install PSWindowsUpdate module: %v", err) |
| 86 | + } |
| 87 | + logger.Println("PSWindowsUpdate module installed successfully") |
| 88 | + } |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func performSystemUpdates(config *Config) { |
| 93 | + logger := logMultiWriter(config.General.LogFile) |
| 94 | + |
| 95 | + err := checkAndInstallPSWindowsUpdate(config) |
| 96 | + if err != nil { |
| 97 | + logger.Printf("Failed to install PSWindowsUpdate module: %v", err) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + var cmd *exec.Cmd |
| 102 | + |
| 103 | + switch config.SystemUpdates.Method { |
| 104 | + case "install_all": |
| 105 | + cmd = exec.Command("powershell", "-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot") |
| 106 | + case "install_specific": |
| 107 | + for _, update := range config.SystemUpdates.SpecificUpdates { |
| 108 | + cmd = exec.Command("powershell", "-Command", fmt.Sprintf("Get-WindowsUpdate -Install -KBArticleID %s", update)) |
| 109 | + err = cmd.Run() |
| 110 | + if err != nil { |
| 111 | + logger.Printf("Failed to install update %s: %v", update, err) |
| 112 | + } else { |
| 113 | + logger.Printf("Installed update %s", update) |
| 114 | + } |
| 115 | + } |
| 116 | + return |
| 117 | + case "install_random": |
| 118 | + updates := selectRandomOrHardcoded(config.SystemUpdates.SpecificUpdates, config.SystemUpdates.SelectionMethod) |
| 119 | + for _, update := range updates { |
| 120 | + cmd = exec.Command("powershell", "-Command", fmt.Sprintf("Get-WindowsUpdate -Install -KBArticleID %s", update)) |
| 121 | + err = cmd.Run() |
| 122 | + if err != nil { |
| 123 | + logger.Printf("Failed to install update %s: %v", update, err) |
| 124 | + } else { |
| 125 | + logger.Printf("Installed update %s", update) |
| 126 | + } |
| 127 | + } |
| 128 | + return |
| 129 | + case "remove_random": |
| 130 | + updates := selectRandomOrHardcoded(config.SystemUpdates.HideUpdates, config.SystemUpdates.SelectionMethod) |
| 131 | + for _, update := range updates { |
| 132 | + cmd = exec.Command("powershell", "-Command", fmt.Sprintf("Remove-WindowsUpdate -KBArticleID %s -NoRestart", update)) |
| 133 | + err = cmd.Run() |
| 134 | + if err != nil { |
| 135 | + logger.Printf("Failed to remove update %s: %v", update, err) |
| 136 | + } else { |
| 137 | + logger.Printf("Removed update %s", update) |
| 138 | + } |
| 139 | + } |
| 140 | + return |
| 141 | + case "hide_updates": |
| 142 | + for _, update := range config.SystemUpdates.HideUpdates { |
| 143 | + cmd = exec.Command("powershell", "-Command", fmt.Sprintf("$HideList = '%s'; Get-WindowsUpdate -KBArticleID $HideList –Hide", update)) |
| 144 | + err = cmd.Run() |
| 145 | + if err != nil { |
| 146 | + logger.Printf("Failed to hide update %s: %v", update, err) |
| 147 | + } else { |
| 148 | + logger.Printf("Hidden update %s", update) |
| 149 | + } |
| 150 | + } |
| 151 | + return |
| 152 | + default: |
| 153 | + logger.Printf("Unknown system update method: %s", config.SystemUpdates.Method) |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + err = cmd.Run() |
| 158 | + if err != nil { |
| 159 | + logger.Printf("Failed to perform system update: %v", err) |
| 160 | + } else { |
| 161 | + logger.Printf("Performed system update with method %s", config.SystemUpdates.Method) |
| 162 | + } |
| 163 | +} |
0 commit comments