From fa71f44c3670a13d1078f71b9ca19535c53ec9a1 Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Wed, 18 Nov 2026 12:38:03 +0100 Subject: [PATCH] Provide the native computer name to Windows script processes --- Cargo.lock | 2 +- crates/agent-windows/Cargo.toml | 2 +- crates/agent-windows/src/native.rs | 18 +++++++++++------- crates/agent-windows/src/process.rs | 16 +++++++++++++++- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2a3f7fb..4e29cbe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2213,7 +2213,7 @@ dependencies = [ [[package]] name = "rmm-agent-windows" -version = "0.1.14" +version = "0.1.15" dependencies = [ "anyhow", "base64 0.22.1", diff --git a/crates/agent-windows/Cargo.toml b/crates/agent-windows/Cargo.toml index 283a6cc..b2b99b1 100644 --- a/crates/agent-windows/Cargo.toml +++ b/crates/agent-windows/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rmm-agent-windows" -version = "0.1.14" +version = "0.1.15" edition.workspace = true license.workspace = true diff --git a/crates/agent-windows/src/native.rs b/crates/agent-windows/src/native.rs index c59e535..6f32897 100644 --- a/crates/agent-windows/src/native.rs +++ b/crates/agent-windows/src/native.rs @@ -250,16 +250,20 @@ fn registry_string(name: &str) -> Result { ); Ok(String::from_utf16(&bytes[..size as usize / 2 - 1])?) } -pub fn snapshot() -> Result { - let started = Utc::now(); - let mut hostname = vec![0u16; 256]; - let mut len = hostname.len() as u32; +pub(crate) fn computer_name(format: COMPUTER_NAME_FORMAT) -> Result { + let mut name = [0u16; 256]; + let mut len = name.len() as u32; ensure!( - unsafe { GetComputerNameExW(ComputerNameDnsHostname, hostname.as_mut_ptr(), &mut len) } - != 0, + unsafe { GetComputerNameExW(format, name.as_mut_ptr(), &mut len) } != 0 + && len > 0 && (len as usize) < name.len(), "HOSTNAME_QUERY" ); - let hostname = String::from_utf16(&hostname[..len as usize])?; + Ok(String::from_utf16(&name[..len as usize])?) +} + +pub fn snapshot() -> Result { + let started = Utc::now(); + let hostname = computer_name(ComputerNameDnsHostname)?; let volume = wide(std::env::var("SystemDrive").context("SYSTEM_DRIVE")? + "\\"); let mut available = 0; let mut total = 0; diff --git a/crates/agent-windows/src/process.rs b/crates/agent-windows/src/process.rs index 46bad00..c4048a1 100644 --- a/crates/agent-windows/src/process.rs +++ b/crates/agent-windows/src/process.rs @@ -299,9 +299,13 @@ pub(crate) fn run_observed( "SYSTEM_DRIVE_REQUIRED" ); let system_drive = &system_text[..2]; + let computer = crate::native::computer_name( + windows_sys::Win32::System::SystemInformation::ComputerNameNetBIOS, + )?; for value in [ - format!("SystemDrive={system_drive}"), + format!("COMPUTERNAME={computer}"), format!("PATH={}\\System32;{}", system.display(), system.display()), + format!("SystemDrive={system_drive}"), format!("SystemRoot={}", system.display()), format!("TEMP={}", cwd.display()), format!("TMP={}", cwd.display()), @@ -455,6 +459,16 @@ pub(crate) fn run_observed( #[cfg(test)] mod tests { use super::*; + #[test] + fn script_process_receives_the_native_computer_name() { + let args = ["-NoProfile", "-NonInteractive", "-Command", + "if (!$env:COMPUTERNAME -or $env:COMPUTERNAME -ine [Environment]::MachineName) { exit 1 }"] + .map(OsString::from); + let output = run(&crate::actions::powershell().unwrap(), &args, + &std::env::temp_dir(), 20, 4096, &AtomicBool::new(false)).unwrap(); + assert_eq!(output.exit_code, Some(0)); + } + #[test] fn arguments_never_become_shell_code_and_trailing_slashes_are_quoted() { assert_eq!(