Run PowerShell commands via script blocks
This commit is contained in:
parent
680d49ddc5
commit
49496f3663
1 changed files with 1 additions and 36 deletions
|
|
@ -682,41 +682,6 @@ fn collect_windows_extended() -> serde_json::Value {
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
const CREATE_NO_WINDOW: u32 = 0x08000000;
|
const CREATE_NO_WINDOW: u32 = 0x08000000;
|
||||||
|
|
||||||
fn parse_utf16_le_bytes(bytes: &[u8]) -> Option<serde_json::Value> {
|
|
||||||
if bytes.len() % 2 != 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let utf16: Vec<u16> = bytes
|
|
||||||
.chunks_exact(2)
|
|
||||||
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
|
|
||||||
.collect();
|
|
||||||
let text = String::from_utf16(&utf16).ok()?;
|
|
||||||
let trimmed = text.trim();
|
|
||||||
if trimmed.is_empty() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
serde_json::from_str(trimmed).ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_powershell_json(bytes: &[u8]) -> Option<serde_json::Value> {
|
|
||||||
let text = decode_powershell_text(bytes)?;
|
|
||||||
if text.is_empty() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
match serde_json::from_str::<serde_json::Value>(&text) {
|
|
||||||
Ok(value) => Some(value),
|
|
||||||
Err(err) => {
|
|
||||||
if cfg!(test) {
|
|
||||||
let preview = text.chars().take(512).collect::<String>();
|
|
||||||
eprintln!(
|
|
||||||
"[collect_windows_extended] falha ao interpretar JSON: {err}; amostra: {preview}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decode_powershell_text(bytes: &[u8]) -> Option<String> {
|
fn decode_powershell_text(bytes: &[u8]) -> Option<String> {
|
||||||
if bytes.is_empty() {
|
if bytes.is_empty() {
|
||||||
return None;
|
return None;
|
||||||
|
|
@ -781,7 +746,7 @@ fn collect_windows_extended() -> serde_json::Value {
|
||||||
|
|
||||||
fn ps(cmd: &str) -> Option<serde_json::Value> {
|
fn ps(cmd: &str) -> Option<serde_json::Value> {
|
||||||
let script = format!(
|
let script = format!(
|
||||||
"$ErrorActionPreference='SilentlyContinue';$ProgressPreference='SilentlyContinue';$result = ({});if ($null -eq $result) {{ return }};$json = $result | ConvertTo-Json -Depth 4 -Compress;if ([string]::IsNullOrWhiteSpace($json)) {{ return }};[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;$json;",
|
"$ErrorActionPreference='SilentlyContinue';$ProgressPreference='SilentlyContinue';$result = & {{\n{}\n}};if ($null -eq $result) {{ return }};$json = $result | ConvertTo-Json -Depth 4 -Compress;if ([string]::IsNullOrWhiteSpace($json)) {{ return }};[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;$json;",
|
||||||
cmd
|
cmd
|
||||||
);
|
);
|
||||||
let encoded = encode_ps_script(&script);
|
let encoded = encode_ps_script(&script);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue