fix: Unable to obtain Windows server information (#963)

* fix: FormatException: Unexpected extension byte (at offset 8) error

* fix: PowerShell script error repair, Windows data parsing repair

* fix: Unable to obtain network card information

* fix: Unable to obtain system startup time

* fix conversation as resolved.
This commit is contained in:
lxdklp
2025-11-22 19:17:40 +08:00
committed by GitHub
parent ca4e65d7a5
commit 75d1a59e77
12 changed files with 455 additions and 215 deletions

View File

@@ -166,25 +166,34 @@ enum WindowsStatusCmdType implements ShellCmdType {
echo('echo ${SystemType.windowsSign}'),
time('[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()'),
/// Get network interface statistics using Windows Performance Counters
/// Get network interface statistics using WMI
///
/// Uses Get-Counter to collect network I/O metrics from all network interfaces:
/// - Collects bytes received and sent per second for all network interfaces
/// Uses WMI Win32_PerfRawData_Tcpip_NetworkInterface for cross-language compatibility:
/// - Takes 2 samples with 1 second interval to calculate rates
/// - Outputs results in JSON format for easy parsing
/// - Counter paths use double backslashes to escape PowerShell string literals
net(
r'Get-Counter -Counter '
r'"\\NetworkInterface(*)\\Bytes Received/sec", '
r'"\\NetworkInterface(*)\\Bytes Sent/sec" '
r'-SampleInterval 1 -MaxSamples 2 | ConvertTo-Json',
r'$s1 = @(Get-WmiObject Win32_PerfRawData_Tcpip_NetworkInterface | '
r'Select-Object Name, BytesReceivedPersec, BytesSentPersec, Timestamp_Sys100NS); '
r'Start-Sleep -Seconds 1; '
r'$s2 = @(Get-WmiObject Win32_PerfRawData_Tcpip_NetworkInterface | '
r'Select-Object Name, BytesReceivedPersec, BytesSentPersec, Timestamp_Sys100NS); '
r'@($s1, $s2) | ConvertTo-Json -Depth 5',
),
sys('(Get-ComputerInfo).OsName'),
cpu(
'Get-WmiObject -Class Win32_Processor | '
'Select-Object Name, LoadPercentage | ConvertTo-Json',
'Select-Object Name, LoadPercentage, NumberOfCores, NumberOfLogicalProcessors | ConvertTo-Json',
),
/// Get system uptime by calculating time since last boot
///
/// Calculates uptime directly in PowerShell to avoid date format parsing issues:
/// - Gets LastBootUpTime from Win32_OperatingSystem
/// - Calculates difference from current time
/// - Returns pre-formatted string: "X days, H:MM" or "H:MM" (if less than 1 day)
/// - Uses ToString('00') for zero-padding to avoid quote escaping issues
uptime(
r"""$up = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime; if ($up.Days -gt 0) { "$($up.Days) days, $($up.Hours):$($up.Minutes.ToString('00'))" } else { "$($up.Hours):$($up.Minutes.ToString('00'))" }""",
),
uptime('(Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime'),
conn('(netstat -an | findstr ESTABLISHED | Measure-Object -Line).Count'),
disk(
'Get-WmiObject -Class Win32_LogicalDisk | '
@@ -213,19 +222,19 @@ enum WindowsStatusCmdType implements ShellCmdType {
),
host(r'Write-Output $env:COMPUTERNAME'),
/// Get disk I/O statistics using Windows Performance Counters
/// Get disk I/O statistics using WMI
///
/// Uses Get-Counter to collect disk I/O metrics from all physical disks:
/// Uses WMI Win32_PerfRawData_PerfDisk_PhysicalDisk:
/// - Monitors read and write bytes per second for all physical disks
/// - Takes 2 samples with 1 second interval to calculate I/O rates
/// - Physical disk counters provide hardware-level I/O statistics
/// - Outputs results in JSON format for parsing
/// - Counter names use wildcard (*) to capture all disk instances
/// - Takes 2 samples with 1 second interval to calculate rates
/// - DiskReadBytesPersec and DiskWriteBytesPersec are cumulative counters
diskio(
r'Get-Counter -Counter '
r'"\\PhysicalDisk(*)\\Disk Read Bytes/sec", '
r'"\\PhysicalDisk(*)\\Disk Write Bytes/sec" '
r'-SampleInterval 1 -MaxSamples 2 | ConvertTo-Json',
r'$s1 = @(Get-WmiObject Win32_PerfRawData_PerfDisk_PhysicalDisk | '
r'Select-Object Name, DiskReadBytesPersec, DiskWriteBytesPersec, Timestamp_Sys100NS); '
r'Start-Sleep -Seconds 1; '
r'$s2 = @(Get-WmiObject Win32_PerfRawData_PerfDisk_PhysicalDisk | '
r'Select-Object Name, DiskReadBytesPersec, DiskWriteBytesPersec, Timestamp_Sys100NS); '
r'@($s1, $s2) | ConvertTo-Json -Depth 5',
),
battery(
'Get-WmiObject -Class Win32_Battery | '
@@ -287,7 +296,7 @@ enum WindowsStatusCmdType implements ShellCmdType {
String get separator => ScriptConstants.getCmdSeparator(name);
@override
String get divider => ScriptConstants.getCmdDivider(name);
String get divider => ScriptConstants.getWindowsCmdDivider(name);
@override
CmdTypeSys get sysType => CmdTypeSys.windows;

View File

@@ -29,6 +29,9 @@ class ScriptConstants {
/// Generate command-specific divider
static String getCmdDivider(String cmdName) => '\necho ${getCmdSeparator(cmdName)}\n\t';
/// Generate command-specific divider for Windows PowerShell
static String getWindowsCmdDivider(String cmdName) => '\n Write-Host "${getCmdSeparator(cmdName)}"\n ';
/// Parse script output into command-specific map
static Map<String, String> parseScriptOutput(String raw) {
final result = <String, String>{};
@@ -102,6 +105,7 @@ exec 2>/dev/null
# DO NOT delete this file while app is running
\$ErrorActionPreference = "SilentlyContinue"
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
''';
}