nullzilla

Task - Lenovo System Update

Feb 11th, 2021 (edited)
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module $env:SyncroModule -DisableNameChecking
  2. Set-ExecutionPolicy Unrestricted -Force
  3. # Requires Windows 10 and Chocolatey
  4. # Documentation for update module: https://github.com/jantari/LSUClient
  5.  
  6. $lsu = "${env:ProgramFiles(x86)}\Lenovo\System Update"
  7. $choco = "$env:allusersprofile\chocolatey\choco.exe"
  8.  
  9. # Trap errors but skip Lenovo's bad URLs
  10. trap {
  11.     if (-Not $_ -match "(404) Not Found") {
  12.         $_; exit 2
  13.     }
  14. }
  15.  
  16. if ((Get-CimInstance -ClassName Win32_ComputerSystem).Manufacturer -notlike 'Lenovo*') {
  17.     Write-Host "System Manufacturer is not Lenovo, exiting"
  18.     exit 0
  19. }
  20.  
  21. $model = (Get-CimInstance -ClassName CIM_ComputerSystem).Model
  22. $notsupportedmodels = @("H420","10181","20309")
  23. foreach ($notsupportedmodel in $notsupportedmodels) {
  24.     if ($model -like "*$notsupportedmodel*") {
  25.         Write-Host "Model is not supported, exiting"
  26.         exit 0
  27.     }
  28. }
  29.  
  30. # Uninstall Lenovo Vantage
  31. if (Test-Path "C:\Program Files (x86)\Lenovo\VantageService") {
  32.     Write-Host "Uninstalling Lenovo Vantage..."
  33.     $vantage = (Get-Item "C:\Program Files (x86)\Lenovo\VantageService\*\Unins*.exe").FullName
  34.     Start-Process "$vantage" -ArgumentList "/SILENT" -Wait
  35. }
  36.  
  37. # Install/Upgrade LSU (if already present choco will reinstall it as its own)
  38. &$choco upgrade lenovo-thinkvantage-system-update -y --no-progress
  39. Write-Host "`n"
  40.  
  41. # Verify Install
  42. if (!(Test-Path "${env:ProgramFiles(x86)}\Lenovo\System Update\tvsu.exe")) {
  43.     Write-Host "LSU not found, install must have failed"
  44.     Rmm-Alert -Category 'Task - Lenovo System Update' -Body "LSU not found, install must have failed"
  45.     exit 2
  46. }
  47.  
  48. # Check for LSUClient module and install/update it
  49. if (!(Get-Module -ListAvailable -Name LSUClient)) {
  50.     Write-Host "LSUClient module not found, installing...`n"
  51.     Install-PackageProvider NuGet -Force
  52.     Set-PSRepository PSGallery -InstallationPolicy Trusted
  53.     Install-Module -Name 'LSUClient'
  54. }
  55. else {
  56.     Write-Host "Checking LSUClient module version...`n"
  57.     if (((Get-Module -ListAvailable -Name LSUClient).Version | Select-Object -first 1) -lt ((Find-Module -Name LSUClient).Version)) {
  58.         Write-Host "Updating LSUClient module..."
  59.         Update-Module -Name 'LSUClient' -Force
  60.     }
  61. }
  62.  
  63. # Find and Apply Updates capable of unattended install
  64. [array]$updates = Get-LSUpdate | Where-Object { $_.Installer.Unattended }
  65.  
  66. if ($updates) {
  67.     Write-Host "Downloading updates...`n"
  68.     $updates | Save-LSUpdate -Verbose
  69.     [array]$results = Install-LSUpdate -Package $updates
  70.     Write-Host "`nUnattended Updates Results:"
  71.     foreach ($item in $results) {
  72.         $item.PSObject.Properties | Where-Object {$_.Value -ne $null} | Format-Table Name,Value
  73.     }
  74.    
  75.     # Determine Action Needed
  76.     if ($results.PendingAction -contains 'REBOOT_SUGGESTED') {
  77.         Write-Host 'Reboot suggested, restarting...'
  78.         shutdown /g /f
  79.     }
  80.     if ($results.PendingAction -contains 'REBOOT_MANDATORY') {
  81.         Write-Host 'Reboot needed, restarting...'
  82.         shutdown /g /f
  83.     }
  84.     if ($results.PendingAction -contains 'SHUTDOWN') {
  85.         Write-Host 'Shutdown needed, shutting down...'
  86.         shutdown /f
  87.     }
  88. }
  89. else {
  90.     Write-Host "`nNo unattended updates found`n"
  91. }
  92.  
  93. # Check for any remaining updates
  94. $attended = Get-LSUpdate | Where-Object { $_.Installer.Unattended -eq $false} | Out-String
  95. if ($attended) {
  96.     Write-Host "`nAttended Updates Found:"
  97.     $attended
  98.     Rmm-Alert -Category 'Task - Lenovo System Update' -Body "Unsupported installer detected, needs manual update<br>$attended"
  99. }
  100. else { Write-Host "`nNo attended updates found" }
  101.  
Add Comment
Please, Sign In to add comment