Files
topgrade/src/steps/powershell.rs

232 lines
7.5 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;
#[cfg(windows)]
use color_eyre::eyre::eyre;
2022-11-11 09:39:29 -05:00
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::{self, print_separator};
use crate::utils::{require_option, which, PathExt};
2019-08-14 21:36:57 +03:00
pub struct Powershell {
path: Option<PathBuf>,
profile: Option<PathBuf>,
is_pwsh: bool,
2019-08-14 21:36:57 +03:00
}
impl Powershell {
pub fn new() -> Self {
if terminal::is_dumb() {
return Self {
path: None,
profile: None,
is_pwsh: false,
};
}
let (path, is_pwsh) = which("pwsh")
.map(|p| (Some(p), true))
.or_else(|| which("powershell").map(|p| (Some(p), false)))
.unwrap_or((None, 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
let profile = path.as_ref().and_then(Self::get_profile);
Self { path, profile, is_pwsh }
}
pub fn is_available(&self) -> bool {
self.path.is_some()
2019-08-14 21:36:57 +03:00
}
#[cfg(windows)]
pub fn windows_powershell() -> Self {
if terminal::is_dumb() {
return Self {
path: None,
profile: None,
is_pwsh: false,
};
}
Self {
path: which("powershell"),
profile: None,
is_pwsh: false,
}
}
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> {
let profile = Self::build_command_internal(path, "Split-Path $PROFILE")
.output_checked_utf8()
.map(|output| output.stdout.trim().to_string())
.and_then(|s| PathBuf::from(s).require())
.ok();
debug!("Found PowerShell profile: {:?}", profile);
profile
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
}
/// Builds an "internal" powershell command
fn build_command_internal(path: &PathBuf, cmd: &str) -> Command {
let mut command = Command::new(path);
command.args(["-NoProfile", "-Command"]);
command.arg(cmd);
command
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
}
/// 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<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 use_sudo && ctx.sudo().is_some() {
let mut cmd = executor.execute(ctx.sudo().as_ref().unwrap());
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.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(["-NoProfile", "-Command"]);
command.arg(cmd);
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
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 = "Update-Module".to_string();
2020-08-23 09:10:09 +03:00
if ctx.config().verbose() {
cmd.push_str(" -Verbose");
2020-08-23 09:10:09 +03:00
}
if ctx.config().yes(Step::Powershell) {
cmd.push_str(" -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
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(())
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(eyre!(
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
"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 mut command = Self::build_command_internal(powershell, "Get-ExecutionPolicy");
let current_policy = command
.output_checked_utf8()
.map(|output| output.stdout.trim().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
debug!("Found PowerShell ExecutionPolicy: {:?}", current_policy);
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
if let Ok(current_policy) = current_policy {
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
// 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 {
fn has_module(&self, module_name: &str) -> bool {
if let Some(powershell) = &self.path {
let cmd = format!("Get-Module -ListAvailable {}", module_name);
2019-08-14 21:36:57 +03:00
return Self::build_command_internal(powershell, &cmd)
.output_checked()
.map(|output| !output.stdout.trim_ascii().is_empty())
.unwrap_or(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
}
pub fn supports_windows_update(&self) -> bool {
self.has_module("PSWindowsUpdate")
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<()> {
use crate::config::UpdatesAutoReboot;
debug_assert!(self.supports_windows_update());
let mut cmd = "Import-Module PSWindowsUpdate; Install-WindowsUpdate -Verbose".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
if ctx.config().accept_all_windows_updates() {
cmd.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
}
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()
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<()> {
println!("{}", t!("Scanning for updates..."));
let cmd = "Start-Process powershell -Verb RunAs -ArgumentList '-Command', \
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
'(Get-CimInstance -Namespace \"Root\\cimv2\\mdm\\dmmap\" \
-ClassName \"MDM_EnterpriseModernAppManagement_AppManagement01\" | \
Invoke-CimMethod -MethodName UpdateScanMethod).ReturnValue'";
self.build_command(ctx, cmd, true)?.status_checked()
}
2019-08-14 21:36:57 +03:00
}