Skip to content

Commit 403854b

Browse files
committed
Improve native CPU architecture detection logic
1 parent 2020f44 commit 403854b

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

1k/1kiss.ps1

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -328,24 +328,14 @@ if ($options.xb.GetType() -eq [string]) {
328328
$options.xb = $options.xb.Split(' ')
329329
}
330330

331-
[VersionEx]$pwsh_ver = [Regex]::Match($PSVersionTable.PSVersion.ToString(), '(\d+\.)+(\*|\d+)').Value
332-
if ([VersionEx]$pwsh_ver -lt [VersionEx]"7.0") {
331+
if (!$Global:IsPwsh7OrLater) {
333332
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
334333
}
335334

336335
$osVerString = if ($IsWin) { "Microsoft Windows $($NtOSVersion.ToString())" } else { $PSVersionTable.OS }
337336

338337
# arm64,x64
339-
# uname -m: arm64/aarch64,x86_64
340-
if ($IsWin) {
341-
# refer: https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor
342-
$__1k_archs = @{9="x64"; 12="arm64"}
343-
$__1k_arch_code = [int](Get-CimInstance Win32_Processor)[0].Architecture
344-
$1k.println("Host architecture code: $__1k_arch_code")
345-
$HOST_CPU = $__1k_archs[$__1k_arch_code]
346-
} else {
347-
$HOST_CPU = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLower()
348-
}
338+
$HOST_CPU = Get-NativeArchitecture
349339

350340
$1k.println("PowerShell $pwsh_ver on $osVerString ($HOST_CPU)")
351341

1k/extensions.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ namespace System
233233
}
234234

235235

236+
$Global:pwsh_ver = [Regex]::Match($PSVersionTable.PSVersion.ToString(), '(\d+\.)+(\*|\d+)').Value
237+
$Global:IsPwsh7OrLater = [VersionEx]$Global:pwsh_ver -ge [VersionEx]"7.0"
238+
236239
function Global:ConvertFrom-Props {
237240
param(
238241
[Parameter(Mandatory = $true)]
@@ -267,3 +270,36 @@ function Global:ConvertTo-Props {
267270
}
268271
return $str_ret
269272
}
273+
274+
function Global:Get-NativeArchitecture {
275+
$arch = $null
276+
if ($Global:IsPwsh7OrLater) {
277+
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLower()
278+
}
279+
else {
280+
# refer to: https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor
281+
try {
282+
$processorInfo = Get-CimInstance -ClassName Win32_Processor -ErrorAction Stop | Select-Object -First 1
283+
284+
switch ($processorInfo.Architecture) {
285+
0 { $arch = "x86" } # 32-bit Intel/AMD
286+
5 { $arch = "arm32" } # 32-bit ARM
287+
12 { $arch = "arm64" } # treat ARM as arm64 target
288+
9 { $arch = "x64" } # 64-bit Intel/AMD
289+
6 { $arch = "ia64" } # Intel Itanium
290+
default { $arch = "x86" } # fallback
291+
}
292+
}
293+
catch {
294+
# Fallback: only 32/64 bit detection if WMI/CIM is not available
295+
if ([System.Environment]::Is64BitOperatingSystem) {
296+
$arch = "x64"
297+
}
298+
else {
299+
$arch = "x86"
300+
}
301+
}
302+
}
303+
304+
return $arch
305+
}

0 commit comments

Comments
 (0)