|
| 1 | +function Use-EasyServiceOptimizer { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Executes operations using the Easy Service Optimizer (eso.exe) tool. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + This function downloads and executes eso.exe to apply or restore startup types and manage service settings. |
| 8 | +
|
| 9 | + .EXAMPLE |
| 10 | + Use-EasyServiceOptimizer -Command '/A' -StartApplication |
| 11 | + Use-EasyServiceOptimizer -Command '/A' -Group 2 |
| 12 | + Use-EasyServiceOptimizer -Command '/A' -ServiceList 'C:\Path\To\service_list.ini' |
| 13 | + Use-EasyServiceOptimizer -Command '/R' |
| 14 | + Use-EasyServiceOptimizer -Command '/A' -ServiceList 'C:\Path\To\service_list.ini' |
| 15 | +
|
| 16 | + .NOTES |
| 17 | + v0.0.1 |
| 18 | + #> |
| 19 | + [CmdletBinding()] |
| 20 | + param ( |
| 21 | + [Parameter(Mandatory = $true, HelpMessage = "Specify the command to execute with eso.exe")] |
| 22 | + [ValidateSet('/A', '/R')] |
| 23 | + [string]$Command, |
| 24 | + |
| 25 | + [Parameter(Mandatory = $false, HelpMessage = "Group settings to apply (1=Default, 2=Safe, 3=Tweaked, 4=Extreme)")] |
| 26 | + [ValidateSet('1', '2', '3', '4')] |
| 27 | + [string]$Group, |
| 28 | + |
| 29 | + [Parameter(Mandatory = $false, HelpMessage = "Path to the service list configuration file")] |
| 30 | + [string]$ServiceList = "", |
| 31 | + |
| 32 | + [Parameter(Mandatory = $false, HelpMessage = "URL for downloading the Easy Service Optimizer tool")] |
| 33 | + [uri]$EsoDownloadUrl = "https://www.sordum.org/files/easy-service-optimizer/eso.zip", |
| 34 | + |
| 35 | + [Parameter(Mandatory = $false, HelpMessage = "Path to the directory where the Easy Service Optimizer tool will be downloaded and extracted")] |
| 36 | + [string]$DownloadPath = "$env:TEMP\Eso", |
| 37 | + |
| 38 | + [Parameter(Mandatory = $false, HelpMessage = "Remove the temporary folder after the operation")] |
| 39 | + [switch]$RemoveEso, |
| 40 | + |
| 41 | + [Parameter(Mandatory = $false, HelpMessage = "Start the Easy Service Optimizer application after extraction")] |
| 42 | + [switch]$StartApplication |
| 43 | + ) |
| 44 | + $EsoZipPath = Join-Path $DownloadPath "eso.zip" |
| 45 | + $EsoExtractPath = Join-Path $DownloadPath "Eso" |
| 46 | + try { |
| 47 | + Write-Host "Creating download directory..." -ForegroundColor Green |
| 48 | + New-Item -Path $DownloadPath -ItemType Directory -Force | Out-Null |
| 49 | + if (!(Test-Path -Path $EsoZipPath)) { |
| 50 | + Write-Host "Downloading Easy Service Optimizer tool..." -ForegroundColor Green |
| 51 | + Invoke-WebRequest -Uri $EsoDownloadUrl -OutFile $EsoZipPath -UseBasicParsing -Verbose |
| 52 | + if ((Get-Item $EsoZipPath).Length -eq 0) { |
| 53 | + throw "The downloaded ZIP file is empty or corrupt." |
| 54 | + } |
| 55 | + } |
| 56 | + Write-Host "Extracting Easy Service Optimizer tool..." -ForegroundColor Green |
| 57 | + if (Test-Path -Path $EsoExtractPath) { |
| 58 | + Remove-Item -Path $EsoExtractPath -Recurse -Force |
| 59 | + } |
| 60 | + try { |
| 61 | + [System.IO.Compression.ZipFile]::ExtractToDirectory($EsoZipPath, $DownloadPath) |
| 62 | + } |
| 63 | + catch { |
| 64 | + throw "Failed to extract the ZIP file. It may be corrupt or incomplete." |
| 65 | + } |
| 66 | + $EsoExecutable = Join-Path $EsoExtractPath "eso.exe" |
| 67 | + if (-Not (Test-Path -Path $EsoExecutable)) { |
| 68 | + throw "eso.exe not found in $EsoExtractPath" |
| 69 | + } |
| 70 | + $Arguments = $Command |
| 71 | + if ($Group) { |
| 72 | + $Arguments += " /G=$Group" |
| 73 | + } |
| 74 | + if ($ServiceList) { |
| 75 | + $Arguments += " $ServiceList" |
| 76 | + } |
| 77 | + Write-Verbose -Message "Starting Easy Service Optimizer with arguments: $Arguments" |
| 78 | + if ($StartApplication) { |
| 79 | + Start-Process -FilePath $EsoExecutable |
| 80 | + } |
| 81 | + else { |
| 82 | + Start-Process -FilePath $EsoExecutable -ArgumentList $Arguments -WindowStyle Hidden -Wait |
| 83 | + } |
| 84 | + } |
| 85 | + catch { |
| 86 | + Write-Error -Message "An error occurred: $_" |
| 87 | + } |
| 88 | + finally { |
| 89 | + Write-Host "Easy Service Optimizer operation '$Command' completed." -ForegroundColor Cyan |
| 90 | + if ($RemoveEso) { |
| 91 | + Write-Warning -Message "Cleaning up, removing the temporary folder..." |
| 92 | + Remove-Item -Path $DownloadPath -Force -Recurse -Verbose |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments