Files
topgrade/src/steps/powershell.rs

208 lines
6.8 KiB
Rust
Raw Normal View History

2021-04-07 13:32:58 +03:00
use std::path::PathBuf;
2019-08-14 21:36:57 +03:00
use std::process::Command;
2022-11-11 09:39:29 -05:00
use color_eyre::eyre::Result;
use rust_i18n::t;
use crate::command::CommandExt;
use crate::execution_context::ExecutionContext;
use crate::terminal::{is_dumb, print_separator};
use crate::utils::{require_option, which};
use crate::Step;
2019-08-14 21:36:57 +03:00
pub struct Powershell {
path: Option<PathBuf>,
profile: Option<PathBuf>,
}
impl Powershell {
pub fn new() -> Self {
let path = which("pwsh").or_else(|| which("powershell")).filter(|_| !is_dumb());
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
let profile = path.as_ref().and_then(Self::get_profile);
2019-08-14 21:36:57 +03:00
Powershell { path, profile }
}
#[cfg(windows)]
pub fn windows_powershell() -> Self {
Powershell {
path: which("powershell").filter(|_| !is_dumb()),
profile: None,
}
}
2019-08-14 21:36:57 +03:00
pub fn profile(&self) -> Option<&PathBuf> {
self.profile.as_ref()
}
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
fn get_profile(path: &PathBuf) -> Option<PathBuf> {
Self::execute_with_command(path, &["-NoProfile", "-Command", "Split-Path $PROFILE"], |stdout| {
Ok(stdout)
})
.ok() // Convert the Result<String> to Option<String>
.and_then(|s| super::super::utils::PathExt::require(PathBuf::from(s)).ok())
}
fn execute_with_command<F>(path: &PathBuf, args: &[&str], f: F) -> Result<String>
where
F: FnOnce(String) -> Result<String>,
{
let output = Command::new(path).args(args).output_checked_utf8()?;
let stdout = output.stdout.trim().to_string();
f(stdout)
}
/// Builds a command with common arguments and optional sudo support.
fn build_command_internal<'a>(
&self,
ctx: &'a ExecutionContext,
additional_args: &[&str],
) -> Result<impl CommandExt + 'a> {
let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?;
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
let executor = &mut ctx.run_type();
let mut command = if let Some(sudo) = ctx.sudo() {
let mut cmd = executor.execute(sudo);
cmd.arg(powershell);
cmd
} else {
executor.execute(powershell)
};
2019-08-14 21:36:57 +03:00
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
#[cfg(windows)]
{
// Check execution policy and return early if it's not set correctly
self.execution_policy_args_if_needed()?;
}
command.args(Self::common_args()).args(additional_args);
Ok(command)
}
2020-07-15 08:47:15 +03:00
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
pub fn update_modules(&self, ctx: &ExecutionContext) -> Result<()> {
print_separator(t!("Powershell Modules Update"));
let mut cmd_args = vec!["Update-Module"];
2020-08-23 09:10:09 +03:00
if ctx.config().verbose() {
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
cmd_args.push("-Verbose");
2020-08-23 09:10:09 +03:00
}
if ctx.config().yes(Step::Powershell) {
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
cmd_args.push("-Force");
2021-04-24 06:07:50 +03:00
}
println!("{}", t!("Updating modules..."));
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
self.build_command_internal(ctx, &cmd_args)?.status_checked()
}
fn common_args() -> &'static [&'static str] {
&["-NoProfile"]
2019-08-14 21:36:57 +03:00
}
#[cfg(windows)]
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
pub fn execution_policy_args_if_needed(&self) -> Result<()> {
if !self.is_execution_policy_set("RemoteSigned") {
Err(color_eyre::eyre::eyre!(
"PowerShell execution policy is too restrictive. \
Please run 'Set-ExecutionPolicy RemoteSigned -Scope CurrentUser' in PowerShell \
(or use Unrestricted/Bypass if you're sure about the security implications)"
))
} else {
Ok(())
}
2020-02-27 22:06:14 +02:00
}
#[cfg(windows)]
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
fn is_execution_policy_set(&self, policy: &str) -> bool {
if let Some(powershell) = &self.path {
// These policies are ordered from most restrictive to least restrictive
let valid_policies = ["Restricted", "AllSigned", "RemoteSigned", "Unrestricted", "Bypass"];
// Find the index of our target policy
let target_idx = valid_policies.iter().position(|&p| p == policy);
let output = Command::new(powershell)
.args(["-NoProfile", "-Command", "Get-ExecutionPolicy"])
.output_checked_utf8();
if let Ok(output) = output {
let current_policy = output.stdout.trim();
// Find the index of the current policy
let current_idx = valid_policies.iter().position(|&p| p == current_policy);
// Check if current policy exists and is at least as permissive as the target
return match (current_idx, target_idx) {
(Some(current), Some(target)) => current >= target,
_ => false,
};
}
}
false
}
}
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
#[cfg(windows)]
impl Powershell {
pub fn supports_windows_update(&self) -> bool {
windows::supports_windows_update(self)
}
2019-08-14 21:36:57 +03:00
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
pub fn windows_update(&self, ctx: &ExecutionContext) -> Result<()> {
windows::windows_update(self, ctx)
}
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
pub fn microsoft_store(&self, ctx: &ExecutionContext) -> Result<()> {
windows::microsoft_store(self, ctx)
}
}
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
#[cfg(windows)]
mod windows {
use super::*;
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
pub fn supports_windows_update(powershell: &Powershell) -> bool {
powershell
.path
.as_ref()
.map(|p| has_module(p, "PSWindowsUpdate"))
.unwrap_or(false)
2019-08-14 21:36:57 +03:00
}
#[cfg(windows)]
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
pub fn windows_update(powershell: &Powershell, ctx: &ExecutionContext) -> Result<()> {
debug_assert!(supports_windows_update(powershell));
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
// Build the full command string
let mut command_str = "Install-WindowsUpdate -Verbose".to_string();
if ctx.config().accept_all_windows_updates() {
command_str.push_str(" -AcceptAll");
}
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
// Pass the command string using the -Command flag
powershell
.build_command_internal(ctx, &["-Command", &command_str])?
.status_checked()
}
pub fn microsoft_store(powershell: &Powershell, ctx: &ExecutionContext) -> Result<()> {
println!("{}", t!("Scanning for updates..."));
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
let update_command = "Start-Process powershell -Verb RunAs -ArgumentList '-Command', \
'(Get-CimInstance -Namespace \"Root\\cimv2\\mdm\\dmmap\" \
-ClassName \"MDM_EnterpriseModernAppManagement_AppManagement01\" | \
Invoke-CimMethod -MethodName UpdateScanMethod).ReturnValue'";
powershell
.build_command_internal(ctx, &["-Command", update_command])?
.status_checked()
}
fix(powershell): update command arguments to include execution policy (#1041) * fix(powershell): update command arguments to include execution policy * fix(powershell): format command arguments for better readability * fix(powershell): refactor command arguments for Windows execution policy and common options * fix(powershell): improve execution policy handling and refactor argument passing * refactor(powershell): streamline command execution and improve profile handling * fix(powershell): improve code formatting for better readability and maintainability * fix(powershell): enhance argument validation for improved error handling * refactor(powershell): simplify Powershell struct methods and enhance command preparation * refactor(powershell): streamline command building and enhance argument handling * refactor(powershell): change add_common_args to use instance method for better encapsulation * refactor(powershell): enhance command building by introducing build_command method and improving argument handling * refactor(powershell): update build_command return type to match executor.execute() output * refactor(powershell): refactor command building by introducing build_command_internal and adding profile getter * refactor(powershell): improve profile retrieval and command execution logic * refactor(powershell): add support for Windows update and Microsoft Store commands * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): change arg method to args for accepting all Windows updates * refactor(powershell): change args method to arg for accepting all Windows updates * refactor(powershell): streamline command construction for Windows updates * refactor(powershell): update args method for has_module function * refactor(powershell): improve execution policy handling and command construction for Windows updates * refactor(powershell): update Microsoft Store update command execution and streamline output handling * refactor(powershell): clean up whitespace and improve code readability in execution policy checks --------- Co-authored-by: nistee <lo9s4b7qp@mozmail.com>
2025-04-10 13:48:53 +02:00
fn has_module(powershell: &PathBuf, command: &str) -> bool {
Command::new(powershell)
.args([
"-NoProfile",
"-Command",
&format!("Get-Module -ListAvailable {}", command),
])
.output_checked_utf8()
.map(|result| !result.stdout.is_empty())
.unwrap_or(false)
}
2019-08-14 21:36:57 +03:00
}