Skip to content

Commit

Permalink
refactor(powershell): enhance command building by introducing build_c…
Browse files Browse the repository at this point in the history
…ommand method and improving argument handling
  • Loading branch information
nistee committed Feb 15, 2025
1 parent d3e149e commit 726c7cd
Showing 1 changed file with 37 additions and 53 deletions.
90 changes: 37 additions & 53 deletions src/steps/powershell.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;

Expand Down Expand Up @@ -47,28 +46,40 @@ impl Powershell {
self.profile.as_ref()
}

pub fn update_modules(&self, ctx: &ExecutionContext) -> Result<()> {
// Changed return type to impl CommandExt to match the type returned by executor.execute().
fn build_command<'a>(&self, ctx: &'a ExecutionContext, additional_args: &[&str]) -> Result<impl CommandExt> {
let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?;
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)
};

#[cfg(windows)]
if let Some(policy_args) = self.execution_policy_args_if_needed() {
command.args(policy_args);
}

command.args(Self::common_args()).args(additional_args);
Ok(command)
}

pub fn update_modules(&self, ctx: &ExecutionContext) -> Result<()> {
print_separator(t!("Powershell Modules Update"));

let mut cmd = vec!["Update-Module"];
let mut cmd_args = vec!["Update-Module"];
if ctx.config().verbose() {
cmd.push("-Verbose");
cmd_args.push("-Verbose");
}
if ctx.config().yes(Step::Powershell) {
cmd.push("-Force");
cmd_args.push("-Force");
}

println!("{}", t!("Updating modules..."));
self.execute_command(ctx, powershell, &cmd)
}

fn execute_command(&self, ctx: &ExecutionContext, powershell: &Path, cmd: &[&str]) -> Result<()> {
ctx.run_type()
.execute(powershell)
.args(Self::common_args())
.args(["-Command", &cmd.join(" ")])
.status_checked()
self.build_command(ctx, &cmd_args)?.status_checked()
}

fn common_args() -> &'static [&'static str] {
Expand Down Expand Up @@ -108,31 +119,25 @@ impl Powershell {

#[cfg(windows)]
pub fn windows_update(&self, ctx: &ExecutionContext) -> Result<()> {
let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?;
debug_assert!(self.supports_windows_update());

let mut command = self.build_powershell_command(ctx, powershell);
command = self.add_common_args(command, &["Install-WindowsUpdate -Verbose"]);

let mut cmd = self.build_command(ctx, &["Install-WindowsUpdate -Verbose"])?;
if ctx.config().accept_all_windows_updates() {
command.arg("-AcceptAll");
cmd.arg("-AcceptAll");
}

command.status_checked()
cmd.status_checked()
}

#[cfg(windows)]
pub fn microsoft_store(&self, ctx: &ExecutionContext) -> Result<()> {
let powershell = require_option(self.path.as_ref(), t!("Powershell is not installed").to_string())?;
let mut command = self.build_powershell_command(ctx, powershell);

println!("{}", t!("Scanning for updates..."));

let update_command = "(Get-CimInstance -Namespace \"Root\\cimv2\\mdm\\dmmap\" -ClassName \"MDM_EnterpriseModernAppManagement_AppManagement01\" | Invoke-CimMethod -MethodName UpdateScanMethod).ReturnValue";

command = self.add_common_args(command, &[update_command]);

command
// Command to scan for updates.
let update_command = "(Get-CimInstance -Namespace \"Root\\cimv2\\mdm\\dmmap\" \
-ClassName \"MDM_EnterpriseModernAppManagement_AppManagement01\" | \
Invoke-CimMethod -MethodName UpdateScanMethod).ReturnValue";
// Fixed: Prepend "-Command" flag to properly pass the command.
self.build_command(ctx, &["-Command", update_command])?
.output_checked_with_utf8(|output| {
if output.stdout.trim() == "0" {
println!(
Expand All @@ -152,33 +157,12 @@ impl Powershell {
}

#[cfg(windows)]
fn build_powershell_command(&self, ctx: &ExecutionContext, powershell: &Path) -> Command {
if let Some(sudo) = ctx.sudo() {
let mut command = ctx.run_type().execute(sudo);
command.arg(powershell);
command
} else {
ctx.run_type().execute(powershell)
}
}

#[cfg(windows)]
fn add_common_args(&self, mut command: Command, additional_args: &[&str]) -> Command {
// Add execution policy if needed.
if let Some(args) = self.execution_policy_args_if_needed() {
command.args(args);
}
command.args(Self::common_args()).args(additional_args);
command
}

#[cfg(windows)]
fn has_module(powershell: &Path, command: &str) -> bool {
fn has_module(powershell: &PathBuf, command: &str) -> bool {
Command::new(powershell)
.args([
.args(&[
"-NoProfile",
"-Command",
&format!("Get-Module -ListAvailable {command}"),
&format!("Get-Module -ListAvailable {}", command),
])
.output_checked_utf8()
.map(|result| !result.stdout.is_empty())
Expand Down

0 comments on commit 726c7cd

Please sign in to comment.