core: 支持无 wmic 下运行

This commit is contained in:
bin456789 2024-11-23 23:50:11 +08:00
parent 4305427b77
commit 823046b6bf
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B
4 changed files with 139 additions and 18 deletions

View File

@ -14,5 +14,8 @@ end_of_line = crlf
[*.{bat,cmd,ps1}]
end_of_line = crlf
[*.ps1]
charset = utf-8-bom
[*.{yml,yaml}]
indent_size = 2

View File

@ -31,12 +31,6 @@ if not exist %tmp% (
md %tmp%
)
rem 24h2 默认禁用了 wmic
where wmic >nul 2>nul
if errorlevel 1 (
DISM /Online /Add-Capability /CapabilityName:WMIC
)
rem 检查是否国内
if not exist %tmp%\geoip (
rem 部分地区 www.cloudflare.com 被墙
@ -70,19 +64,34 @@ if not exist "%tags%" (
rem wmic os get osarchitecture 显示中文
rem wmic ComputerSystem get SystemType 显示英文
for /f "tokens=2 delims==" %%a in ('wmic os get BuildNumber /format:list ^| find "BuildNumber"') do (
set /a BuildNumber=%%a
rem SystemType
rem windows 11 24h2 没有 wmic
rem 有的系统精简了 powershell
where wmic >nul 2>&1
if not errorlevel 1 (
for /f "tokens=*" %%a in ('wmic ComputerSystem get SystemType ^| find /i "based"') do (
set "SystemType=%%a"
)
) else (
for /f "delims=" %%a in ('powershell -NoLogo -NoProfile -NonInteractive -Command "(Get-WmiObject win32_computersystem).SystemType"') do (
set "SystemType=%%a"
)
)
rem BuildNumber
for /f "tokens=3" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentBuildNumber') do (
set /a BuildNumber=%%a
)
set CygwinEOL=1
wmic ComputerSystem get SystemType | find "ARM" > nul
echo !SystemType! | find "ARM" > nul
if not errorlevel 1 (
if !BuildNumber! GEQ 22000 (
set CygwinEOL=0
)
) else (
wmic ComputerSystem get SystemType | find "x64" > nul
echo !SystemType! | find "x64" > nul
if not errorlevel 1 (
if !BuildNumber! GEQ 9600 (
set CygwinEOL=0

View File

@ -443,6 +443,80 @@ run_with_del_cr_template() {
fi
}
_wmic() {
if is_have_cmd wmic; then
# 如果参数没有 GET添加 GET防止以下报错
# wmic memorychip /format:list
# 此级别的开关异常。
has_get=false
for i in "$@"; do
# 如果参数有 GET
if [ "$(to_upper <<<"$i")" = GET ]; then
has_get=true
break
fi
done
# 输出为 /format:list 格式
if $has_get; then
command wmic "$@" /format:list
else
command wmic "$@" get /format:list
fi
return
fi
# powershell wmi 默认参数
local namespace='root\cimv2'
local class=
local filter=
local props=
# namespace
if [[ "$(to_upper <<<"$1")" = /NAMESPACE* ]]; then
# 删除引号,删除 \\
namespace=$(cut -d: -f2 <<<"$1" | sed -e "s/[\"']//g" -e 's/\\\\//g')
shift
fi
# class
if [[ "$(to_upper <<<"$1")" = PATH ]]; then
class=$2
shift 2
else
case "$(to_lower <<<"$1")" in
nicconfig) class=Win32_NetworkAdapterConfiguration ;;
memorychip) class=Win32_PhysicalMemory ;;
*) class=Win32_$1 ;;
esac
shift
fi
# filter
if [[ "$(to_upper <<<"$1")" = WHERE ]]; then
filter=$2
shift 2
fi
# props
if [[ "$(to_upper <<<"$1")" = GET ]]; then
props=$2
shift 2
fi
if ! [ -f "$tmp/wmic.ps1" ]; then
curl -Lo "$tmp/wmic.ps1" "$confhome/wmic.ps1"
fi
# shellcheck disable=SC2046
powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass \
-File "$(cygpath -w "$tmp/wmic.ps1")" \
-Namespace $namespace \
-Class $class \
$([ -n "$filter" ] && echo "-Filter $filter") \
$([ -n "$props" ] && echo "-Properties $props")
}
is_virt() {
if [ -z "$_is_virt" ]; then
if is_in_windows; then
@ -450,7 +524,7 @@ is_virt() {
# https://sources.debian.org/src/hw-detect/1.159/hw-detect.finish-install.d/08hw-detect/
vmstr='VMware|Virtual|Virtualization|VirtualBox|VMW|Hyper-V|Bochs|QEMU|KVM|OpenStack|KubeVirt|innotek|Xen|Parallels|BHYVE'
for name in ComputerSystem BIOS BaseBoard; do
if wmic $name get /format:list | grep -Eiw $vmstr; then
if wmic $name | grep -Eiw $vmstr; then
_is_virt=true
break
fi
@ -458,8 +532,8 @@ is_virt() {
# 没有风扇和温度信息,大概是虚拟机
if [ -z "$_is_virt" ] &&
! wmic /namespace:'\\root\cimv2' PATH Win32_Fan 2>/dev/null | grep -q Name &&
! wmic /namespace:'\\root\wmi' PATH MSAcpi_ThermalZoneTemperature 2>/dev/null | grep -q Name; then
! wmic /namespace:'\\root\cimv2' PATH Win32_Fan 2>/dev/null | grep -q ^Name &&
! wmic /namespace:'\\root\wmi' PATH MSAcpi_ThermalZoneTemperature 2>/dev/null | grep -q ^Name; then
_is_virt=true
fi
else
@ -1702,7 +1776,7 @@ install_pkg() {
return 0
fi
# busybox grep 无法 grep -oP
# busybox grep 不支持 -oP
if [ "$cmd" = grep ] && is_have_cmd apk && $cmd |& grep -wq BusyBox; then
return 0
fi
@ -1748,7 +1822,7 @@ check_ram() {
)
if is_in_windows; then
ram_size=$(wmic memorychip get capacity | tail +2 | awk '{sum+=$1} END {print sum/1024/1024}')
ram_size=$(wmic memorychip get capacity | awk -F= '{sum+=$2} END {print sum/1024/1024}')
else
# lsmem最准确但 centos7 arm 和 alpine 不能用debian 9 util-linux 没有 lsmem
# arm 24g dmidecode 显示少了128m
@ -2094,7 +2168,7 @@ collect_netconf() {
gateway=$(awk '{print $6}' <<<"$route")
fi
config=$(wmic nicconfig where InterfaceIndex=$id get MACAddress,IPAddress,IPSubnet,DefaultIPGateway /format:list)
config=$(wmic nicconfig where InterfaceIndex=$id get MACAddress,IPAddress,IPSubnet,DefaultIPGateway)
# 排除 IP/子网/网关/MAC 为空的
if grep -q '=$' <<<"$config"; then
continue
@ -3425,8 +3499,7 @@ if is_in_windows; then
# x64-based PC
# ARM-based PC
# ARM64-based PC
basearch=$(wmic ComputerSystem get SystemType /format:list |
grep '=' | cut -d= -f2 | cut -d- -f1)
basearch=$(wmic ComputerSystem get SystemType | grep '=' | cut -d= -f2 | cut -d- -f1)
else
# archlinux 云镜像没有 arch 命令
# https://en.wikipedia.org/wiki/Uname

36
wmic.ps1 Normal file
View File

@ -0,0 +1,36 @@
param(
[string]$Namespace,
[string]$Class,
[string]$Filter,
[string]$Properties
)
$propertiesToDisplay = if ($Properties) { $Properties.Split(",") } else { @("*") }
$wmiQuery = @{
Namespace = $Namespace
Class = $Class
}
if ($Filter) {
$wmiQuery.Filter = $Filter
}
Get-WmiObject @wmiQuery | ForEach-Object {
$_.PSObject.Properties | Where-Object {
-not $_.Name.StartsWith("__") -and
($propertiesToDisplay -contains $_.Name -or $propertiesToDisplay -contains "*")
} | ForEach-Object {
$name = $_.Name
$value = $_.Value
# 改成 wmic 的输出格式
if ($value -is [Array]) {
$formattedValue = ($value | ForEach-Object { "`"$_`"" }) -join ","
Write-Output "$name={$formattedValue}"
}
else {
Write-Output "$name=$value"
}
}
}