From fec08a5ad125a309fbb504c16dc06d89304ab9de Mon Sep 17 00:00:00 2001 From: Andre Toerien Date: Sat, 27 Sep 2025 19:55:56 +0200 Subject: [PATCH] Move step logic out of Powershell struct (#1345) --- src/steps/generic.rs | 21 ++++++++- src/steps/os/windows.rs | 49 ++++++++++++++++++--- src/steps/powershell.rs | 98 ++++++----------------------------------- 3 files changed, 77 insertions(+), 91 deletions(-) diff --git a/src/steps/generic.rs b/src/steps/generic.rs index ec9a4db2..a402c297 100644 --- a/src/steps/generic.rs +++ b/src/steps/generic.rs @@ -1065,7 +1065,26 @@ pub fn run_powershell(ctx: &ExecutionContext) -> Result<()> { print_separator(t!("Powershell Modules Update")); - powershell.update_modules(ctx) + let mut cmd = "Update-Module".to_string(); + + if ctx.config().verbose() { + cmd.push_str(" -Verbose"); + } + if ctx.config().yes(Step::Powershell) { + cmd.push_str(" -Force"); + } + + println!("{}", t!("Updating modules...")); + + if powershell.is_pwsh() { + // For PowerShell Core, run Update-Module without sudo since it defaults to CurrentUser scope + // and Update-Module updates all modules regardless of their original installation scope + powershell.build_command(ctx, &cmd, false)?.status_checked() + } else { + // For (Windows) PowerShell, use sudo if available since it defaults to AllUsers scope + // and may need administrator privileges + powershell.build_command(ctx, &cmd, true)?.status_checked() + } } enum Hx { diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index e253ae9f..b6dfa75e 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -3,15 +3,16 @@ use std::{ffi::OsStr, process::Command}; use color_eyre::eyre::Result; use etcetera::base_strategy::BaseStrategy; +use rust_i18n::t; use tracing::debug; use crate::command::CommandExt; +use crate::config::UpdatesAutoReboot; use crate::execution_context::ExecutionContext; use crate::step::Step; use crate::terminal::{print_separator, print_warning}; use crate::utils::{require, which}; use crate::{error::SkipStep, steps::git::RepoStep}; -use rust_i18n::t; pub fn run_chocolatey(ctx: &ExecutionContext) -> Result<()> { let choco = require("choco")?; @@ -215,15 +216,27 @@ pub fn windows_update(ctx: &ExecutionContext) -> Result<()> { print_separator(t!("Windows Update")); - if powershell.supports_windows_update() { - powershell.windows_update(ctx) - } else { + if !powershell.has_module("PSWindowsUpdate") { print_warning(t!( "The PSWindowsUpdate PowerShell module isn't installed so Topgrade can't run Windows Update.\nInstall PSWindowsUpdate by running `Install-Module PSWindowsUpdate` in PowerShell." )); - Err(SkipStep(t!("PSWindowsUpdate is not installed").to_string()).into()) + return Err(SkipStep(t!("PSWindowsUpdate is not installed").to_string()).into()); } + + let mut cmd = "Import-Module PSWindowsUpdate; Install-WindowsUpdate -Verbose".to_string(); + + if ctx.config().accept_all_windows_updates() { + cmd.push_str(" -AcceptAll"); + } + + match ctx.config().windows_updates_auto_reboot() { + UpdatesAutoReboot::Yes => cmd.push_str(" -AutoReboot"), + UpdatesAutoReboot::No => cmd.push_str(" -IgnoreReboot"), + UpdatesAutoReboot::Ask => (), // Prompting is the default for Install-WindowsUpdate + } + + powershell.build_command(ctx, &cmd, true)?.status_checked() } pub fn microsoft_store(ctx: &ExecutionContext) -> Result<()> { @@ -231,7 +244,31 @@ pub fn microsoft_store(ctx: &ExecutionContext) -> Result<()> { print_separator(t!("Microsoft Store")); - powershell.microsoft_store(ctx) + println!("{}", t!("Scanning for updates...")); + + // Scan for updates using the MDM UpdateScanMethod + // This method is also available for non-MDM devices + let cmd = r#"(Get-CimInstance -Namespace "Root\cimv2\mdm\dmmap" -ClassName "MDM_EnterpriseModernAppManagement_AppManagement01" | Invoke-CimMethod -MethodName UpdateScanMethod).ReturnValue"#; + + powershell + .build_command(ctx, cmd, true)? + .output_checked_with_utf8(|output| { + if !output.status.success() { + return Err(()); + } + let ret_val = output.stdout.trim(); + debug!("Command return value: {}", ret_val); + if ret_val == "0" { + Ok(()) + } else { + Err(()) + } + })?; + println!( + "{}", + t!("Success, Microsoft Store apps are being updated in the background") + ); + Ok(()) } pub fn reboot(ctx: &ExecutionContext) -> Result<()> { diff --git a/src/steps/powershell.rs b/src/steps/powershell.rs index eb6d5e19..7bb1515b 100644 --- a/src/steps/powershell.rs +++ b/src/steps/powershell.rs @@ -4,12 +4,10 @@ use std::process::Command; #[cfg(windows)] use color_eyre::eyre::eyre; use color_eyre::eyre::Result; -use rust_i18n::t; use tracing::debug; use crate::command::CommandExt; use crate::execution_context::ExecutionContext; -use crate::step::Step; use crate::terminal; use crate::utils::{which, PathExt}; @@ -56,8 +54,12 @@ impl Powershell { self.profile = profile; } + pub fn is_pwsh(&self) -> bool { + self.is_pwsh + } + /// Builds an "internal" powershell command - fn build_command_internal(&self, cmd: &str) -> Command { + pub fn build_command_internal(&self, cmd: &str) -> Command { let mut command = Command::new(&self.path); command.args(["-NoProfile", "-Command"]); @@ -74,7 +76,12 @@ impl Powershell { /// Builds a "primary" powershell command (uses dry-run if required): /// {powershell} -NoProfile -Command {cmd} - fn build_command<'a>(&self, ctx: &'a ExecutionContext, cmd: &str, use_sudo: bool) -> Result { + pub fn build_command<'a>( + &self, + ctx: &'a ExecutionContext, + cmd: &str, + use_sudo: bool, + ) -> Result { // if use_sudo and sudo is available, use it, otherwise run directly let mut command = match ctx.sudo() { Some(sudo) if use_sudo => sudo.execute(ctx, &self.path)?, @@ -99,33 +106,8 @@ impl Powershell { Ok(command) } - pub fn update_modules(&self, ctx: &ExecutionContext) -> Result<()> { - let mut cmd = "Update-Module".to_string(); - - if ctx.config().verbose() { - cmd.push_str(" -Verbose"); - } - if ctx.config().yes(Step::Powershell) { - cmd.push_str(" -Force"); - } - - println!("{}", t!("Updating modules...")); - - if self.is_pwsh { - // For PowerShell Core, run Update-Module without sudo since it defaults to CurrentUser scope - // and Update-Module updates all modules regardless of their original installation scope - self.build_command(ctx, &cmd, false)?.status_checked()?; - } else { - // For (Windows) PowerShell, use sudo if available since it defaults to AllUsers scope - // and may need administrator privileges - self.build_command(ctx, &cmd, true)?.status_checked()?; - } - - Ok(()) - } - #[cfg(windows)] - pub fn execution_policy_args_if_needed(&self) -> Result<()> { + fn execution_policy_args_if_needed(&self) -> Result<()> { if !self.is_execution_policy_set("RemoteSigned") { Err(eyre!( "PowerShell execution policy is too restrictive. \ @@ -163,11 +145,9 @@ impl Powershell { } }) } -} -#[cfg(windows)] -impl Powershell { - fn has_module(&self, module_name: &str) -> bool { + #[cfg(windows)] + pub fn has_module(&self, module_name: &str) -> bool { let cmd = format!("Get-Module -ListAvailable {}", module_name); self.build_command_internal(&cmd) @@ -175,54 +155,4 @@ impl Powershell { .map(|output| !output.stdout.trim_ascii().is_empty()) .unwrap_or(false) } - - pub fn supports_windows_update(&self) -> bool { - self.has_module("PSWindowsUpdate") - } - - pub fn windows_update(&self, ctx: &ExecutionContext) -> Result<()> { - use crate::config::UpdatesAutoReboot; - - debug_assert!(self.supports_windows_update()); - - let mut cmd = "Import-Module PSWindowsUpdate; Install-WindowsUpdate -Verbose".to_string(); - - if ctx.config().accept_all_windows_updates() { - cmd.push_str(" -AcceptAll"); - } - - match ctx.config().windows_updates_auto_reboot() { - UpdatesAutoReboot::Yes => cmd.push_str(" -AutoReboot"), - UpdatesAutoReboot::No => cmd.push_str(" -IgnoreReboot"), - UpdatesAutoReboot::Ask => (), // Prompting is the default for Install-WindowsUpdate - } - - self.build_command(ctx, &cmd, true)?.status_checked() - } - - pub fn microsoft_store(&self, ctx: &ExecutionContext) -> Result<()> { - println!("{}", t!("Scanning for updates...")); - - // Scan for updates using the MDM UpdateScanMethod - // This method is also available for non-MDM devices - let cmd = r#"(Get-CimInstance -Namespace "Root\cimv2\mdm\dmmap" -ClassName "MDM_EnterpriseModernAppManagement_AppManagement01" | Invoke-CimMethod -MethodName UpdateScanMethod).ReturnValue"#; - - self.build_command(ctx, cmd, true)?.output_checked_with_utf8(|output| { - if !output.status.success() { - return Err(()); - } - let ret_val = output.stdout.trim(); - debug!("Command return value: {}", ret_val); - if ret_val == "0" { - Ok(()) - } else { - Err(()) - } - })?; - println!( - "{}", - t!("Success, Microsoft Store apps are being updated in the background") - ); - Ok(()) - } }