topgrade-rs / topgrade

Upgrade all the things
GNU General Public License v3.0
2.08k stars 140 forks source link

Windows system update fails #531

Open selfagency opened 1 year ago

selfagency commented 1 year ago

Erroneous Behavior

Import-Module : File
C:\Users\dsieradski\OneDrive\Documents\PowerShell\Modules\PSWindowsUpdate\2.2.0.3\PSWindowsUpdate.psm1 cannot be
loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [Import-Module], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand
Install-WindowsUpdate : The 'Install-WindowsUpdate' command was found in the module 'PSWindowsUpdate', but the
module could not be loaded. For more information, run 'Import-Module PSWindowsUpdate'.
At line:1 char:32
+ Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate ...
+                                ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Install-WindowsUpdate:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

Windows update failed:
   0: Command failed: `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE -NoProfile -Command 'Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose'`
   1: `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE` failed: exit code: 1

Expected Behavior

For the update to run as expected.

Steps to reproduce

topgrade --only system

Possible Cause (Optional)

C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE is a restricted and outdated version of PowerShell. You should use pwsh instead, which is the modern version, if it's available and present in the path.

Problem persists without calling from topgrade

Did you run topgrade through Remote Execution

If yes, does the issue still occur when you run topgrade directlly in your remote host

Additional Details

Verbose Output (topgrade -v)

``` ❯ topgrade -v --only system DEBUG Configuration at C:\Users\dsieradski\AppData\Roaming\topgrade.toml DEBUG No include paths found in C:\Users\dsieradski\AppData\Roaming\topgrade.toml DEBUG Loaded configuration: ConfigFile { include: Some(Include { paths: None }), misc: Some(Misc { pre_sudo: None, sudo_command: None, git_repos: None, predefined_git_repos: None, disable: None, ignore_failures: None, remote_topgrades: None, remote_topgrade_path: None, ssh_arguments: None, git_arguments: None, tmux_arguments: None, set_title: None, display_time: None, assume_yes: Some(true), yay_arguments: None, aura_aur_arguments: None, aura_pacman_arguments: None, no_retry: None, run_in_tmux: None, cleanup: None, notify_each_step: None, accept_all_windows_updates: None, skip_notify: None, bashit_branch: None, only: None, no_self_update: None }), pre_commands: Some({}), post_commands: Some({}), commands: Some({}), python: Some(Python { enable_pip_review: None, enable_pip_review_local: None, enable_pipupgrade: None, pipupgrade_arguments: None }), composer: Some(Composer { self_update: None }), brew: Some(Brew { greedy_cask: None, autoremove: None }), linux: Some(Linux { yay_arguments: None, aura_aur_arguments: None, aura_pacman_arguments: None, arch_package_manager: None, show_arch_news: None, garuda_update_arguments: None, trizen_arguments: None, pikaur_arguments: None, pamac_arguments: None, dnf_arguments: None, nix_arguments: None, apt_arguments: None, enable_tlmgr: None, redhat_distro_sync: None, suse_dup: None, rpm_ostree: None, emerge_sync_flags: None, emerge_update_flags: None, home_manager_arguments: None }), git: Some(Git { max_concurrency: None, arguments: None, repos: None, pull_predefined: None }), windows: Some(Windows { accept_all_updates: None, self_rename: None, open_remotes_in_new_terminal: None, enable_winget: Some(true), wsl_update_pre_release: None, wsl_update_use_web_download: None }), npm: Some(NPM { use_sudo: None }), yarn: Some(Yarn { use_sudo: None }), vim: Some(Vim { force_plug_update: None }), firmware: Some(Firmware { upgrade: None }), vagrant: Some(Vagrant { directories: None, power_on: None, always_suspend: None }), flatpak: Some(Flatpak { use_sudo: None }), distrobox: Some(Distrobox { use_root: None, containers: None }) } DEBUG Version: 12.0.2 DEBUG OS: x86_64-pc-windows-msvc DEBUG Args { inner: ["C:\\Users\\dsieradski\\scoop\\apps\\topgrade\\current\\topgrade.exe", "-v", "--only", "system"] } DEBUG Binary path: Ok("C:\\Users\\dsieradski\\scoop\\apps\\topgrade\\current\\topgrade.exe") DEBUG Self Update: true DEBUG Detected "C:\\Program Files\\Git\\cmd\\git.EXE" as "git" DEBUG Detected "C:\\Program Files\\PowerShell\\7\\pwsh.EXE" as "pwsh" DEBUG Executing command `C:\Program Files\PowerShell\7\pwsh.EXE -NoProfile -Command 'Split-Path $profile'` DEBUG Path "C:\\Users\\dsieradski\\OneDrive\\Documents\\PowerShell" exists DEBUG Path "C:\\Users\\dsieradski\\AppData\\Roaming\\.emacs.d" doesn't exist DEBUG Cannot find "doas" DEBUG Cannot find "please" DEBUG Cannot find "sudo" DEBUG Cannot find "gsudo" DEBUG Cannot find "pkexec" ── 09:05:30 - Self update ────────────────────────────────────────────────────── DEBUG starting new connection: https://api.github.com/ DEBUG resolving host="api.github.com" DEBUG connecting to 140.82.114.6:443 DEBUG connected to 140.82.114.6:443 DEBUG No cached session for DnsName("api.github.com") DEBUG Not resuming any session DEBUG Using ciphersuite TLS13_AES_128_GCM_SHA256 DEBUG Not resuming DEBUG TLS1.3 encrypted extensions: [ServerNameAck, Protocols([ProtocolName(6832)])] DEBUG ALPN protocol is Some(b"h2") DEBUG binding client connection DEBUG client connection bound DEBUG send frame=Settings { flags: (0x0), enable_push: 0, initial_window_size: 2097152, max_frame_size: 16384 } DEBUG Connection{peer=Client}: new DEBUG Connection{peer=Client}: send frame=WindowUpdate { stream_id: StreamId(0), size_increment: 5177345 } DEBUG pooling idle connection for ("https", api.github.com) DEBUG Connection{peer=Client}: send frame=Headers { stream_id: StreamId(1), flags: (0x5: END_HEADERS | END_STREAM) } DEBUG Connection{peer=Client}: received frame=Settings { flags: (0x0), max_concurrent_streams: 100, initial_window_size: 67108864, max_frame_size: 65536, enable_connect_protocol: 1 } DEBUG Connection{peer=Client}: send frame=Settings { flags: (0x1: ACK) } DEBUG Connection{peer=Client}: received frame=Settings { flags: (0x1: ACK) } DEBUG Connection{peer=Client}: received settings ACK; applying Settings { flags: (0x0), enable_push: 0, initial_window_size: 2097152, max_frame_size: 16384 } DEBUG Connection{peer=Client}: received frame=Headers { stream_id: StreamId(1), flags: (0x4: END_HEADERS) } DEBUG Connection{peer=Client}: received frame=Data { stream_id: StreamId(1) } DEBUG Connection{peer=Client}: received frame=Data { stream_id: StreamId(1), flags: (0x1: END_STREAM) } DEBUG Connection{peer=Client}: close Topgrade is up-to-date DEBUG Step "Windows update" DEBUG Detected "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.EXE" as "powershell" DEBUG Executing command `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE -NoProfile -Command 'Get-Module -ListAvailable PSWindowsUpdate'` ── 09:05:41 - Windows Update ─────────────────────────────────────────────────── DEBUG Executing command `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE -NoProfile -Command 'Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose'` Import-Module : File C:\Users\dsieradski\OneDrive\Documents\PowerShell\Modules\PSWindowsUpdate\2.2.0.3\PSWindowsUpdate.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:1 + Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [Import-Module], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand Install-WindowsUpdate : The 'Install-WindowsUpdate' command was found in the module 'PSWindowsUpdate', but the module could not be loaded. For more information, run 'Import-Module PSWindowsUpdate'. At line:1 char:32 + Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate ... + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Install-WindowsUpdate:String) [], CommandNotFoundException + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule DEBUG Command failed: Err( 0: Command failed: `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE -NoProfile -Command 'Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose'` 1: `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE` failed: exit code: 1 Location: src\steps\powershell.rs:122 Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it. Run with RUST_BACKTRACE=full to include source snippets.) DEBUG Step "Windows update" failed: 0: Command failed: `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE -NoProfile -Command 'Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose'` 1: `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE` failed: exit code: 1 Location: src\steps\powershell.rs:122 Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it. Run with RUST_BACKTRACE=full to include source snippets. Windows update failed: 0: Command failed: `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE -NoProfile -Command 'Import-Module PSWindowsUpdate; Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose'` 1: `C:\windows\System32\WindowsPowerShell\v1.0\powershell.EXE` failed: exit code: 1 Location: src\steps\powershell.rs:122 ```
selfagency commented 1 year ago

As I suspected, when run with pwsh instead of Windows PowerShell 1.0, it works.

selfagency commented 1 year ago

Found a workaround. Open Windows PowerShell as administrator and run set-executionpolicy remotesigned from the prompt.

niStee commented 9 months ago

@selfagency, in #671 an evaluated shell is called to import and run the PSWindowsUpdate module. Let me know if this solves your problem.

niStee commented 4 months ago

842