Provide the native computer name to Windows script processes

This commit is contained in:
2026-11-18 12:38:03 +01:00
parent 5889964ea1
commit fa71f44c36
4 changed files with 28 additions and 10 deletions
+11 -7
View File
@@ -250,16 +250,20 @@ fn registry_string(name: &str) -> Result<String> {
);
Ok(String::from_utf16(&bytes[..size as usize / 2 - 1])?)
}
pub fn snapshot() -> Result<Value> {
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<String> {
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<Value> {
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;
+15 -1
View File
@@ -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!(