Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/OperatingSystems/Windows.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace KnotsPHP\System\OperatingSystems;

use KnotsPHP\System\Contracts\OperatingSystemContract;
use KnotsPHP\System\Exceptions\InvalidArgumentException;
use KnotsPHP\System\Helpers\Shell;

final class Windows implements OperatingSystemContract
Expand All @@ -25,10 +24,17 @@ public function __construct()

private function retrieveFromSwVers(): void
{
$result = Shell::exec('wmic os get Caption, Version, BuildNumber /format:list');
// BuildNumber=22631
// Caption=Microsoft Windows 11 Famille
// wmic was removed in Windows 11 24H2 / Server 2025; use PowerShell instead.
// -EncodedCommand sidesteps cmd.exe nested-quote issues. Decoded script:
// $o=Get-CimInstance Win32_OperatingSystem;
// $v=[Environment]::OSVersion.Version;
// 'Caption='+$o.Caption;
// 'Version='+$v.Major+'.'+$v.Minor+'.'+$v.Build;
// 'BuildNumber='+$v.Build
$result = Shell::exec('powershell -NoProfile -EncodedCommand JABvAD0ARwBlAHQALQBDAGkAbQBJAG4AcwB0AGEAbgBjAGUAIABXAGkAbgAzADIAXwBPAHAAZQByAGEAdABpAG4AZwBTAHkAcwB0AGUAbQA7ACQAdgA9AFsARQBuAHYAaQByAG8AbgBtAGUAbgB0AF0AOgA6AE8AUwBWAGUAcgBzAGkAbwBuAC4AVgBlAHIAcwBpAG8AbgA7ACcAQwBhAHAAdABpAG8AbgA9ACcAKwAkAG8ALgBDAGEAcAB0AGkAbwBuADsAJwBWAGUAcgBzAGkAbwBuAD0AJwArACQAdgAuAE0AYQBqAG8AcgArACcALgAnACsAJAB2AC4ATQBpAG4AbwByACsAJwAuACcAKwAkAHYALgBCAHUAaQBsAGQAOwAnAEIAdQBpAGwAZABOAHUAbQBiAGUAcgA9ACcAKwAkAHYALgBCAHUAaQBsAGQA');
// Caption=Microsoft Windows 11 Pro
// Version=10.0.22631
// BuildNumber=22631

$lines = explode(PHP_EOL, $result);

Expand All @@ -41,7 +47,7 @@ private function retrieveFromSwVers(): void

$this->cached_name = 'Windows';

if (str_contains($infos['Caption'], 'Server')) {
if (str_contains($infos['Caption'] ?? '', 'Server')) {
// Server 2022
$this->cached_edition = implode(' ', array_slice(explode(' ', $infos['Caption']), 2));
$this->cached_version = $this->getWindowsVersion($infos['Version'] ?? '');
Expand Down Expand Up @@ -93,9 +99,9 @@ public function getWindowsVersion(string $versionString): ?string
// 10.0.22631 / 10.0.20348
$versionParts = explode('.', $versionString);

// Check if the version string is valid
// Bail out gracefully on malformed input rather than crashing the constructor.
if (count($versionParts) < 2) {
throw new InvalidArgumentException('Invalid Windows version string: '.$versionString);
return null;
}

// Extract major and minor version numbers
Expand Down
Loading