From c3a5fff7600924011111c9c1da85e15ba68938d3 Mon Sep 17 00:00:00 2001 From: bin456789 Date: Tue, 28 Apr 2026 07:33:08 +0800 Subject: [PATCH] =?UTF-8?q?core:=20=E4=BC=98=E5=8C=96=20powershell=20?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 避免 wmic where 条件有空格时报错 - 减少 ForEach-Object 以提高速度 --- reinstall.sh | 5 ++-- wmic.ps1 | 69 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/reinstall.sh b/reinstall.sh index fabe9c2..e1a1c12 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -643,13 +643,12 @@ wmic() { 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") + ${filter:+"-Filter"} ${filter:+"$filter"} \ + ${props:+"-Properties"} ${props:+"$props"} } is_virt() { diff --git a/wmic.ps1 b/wmic.ps1 index cd99735..80437ba 100644 --- a/wmic.ps1 +++ b/wmic.ps1 @@ -1,36 +1,59 @@ param( - [string]$Namespace, - [string]$Class, + [string]$Namespace = "root\cimv2", + [Parameter(Mandatory = $true)] [string]$Class, [string]$Filter, [string]$Properties ) -$propertiesToDisplay = if ($Properties) { $Properties.Split(",") } else { @("*") } - -$wmiQuery = @{ - Namespace = $Namespace - Class = $Class +# 预处理属性列表:如果有输入则处理成数组,否则保持为空数组 +[string[]]$propertyList = if ($Properties) { + $Properties.Split(",") | ForEach-Object { $_.Trim() } +} +else { + @() } -if ($Filter) { - $wmiQuery.Filter = $Filter +# 检查是否支持 Get-Cimresult +$isSupportCim = [bool](Get-Command Get-Cimresult -ErrorAction SilentlyContinue) + +# 构造查询参数 +$queryParams = @{ Namespace = $Namespace } +if ($isSupportCim) { $queryParams.ClassName = $Class } else { $queryParams.Class = $Class } +if ($Filter) { $queryParams.Filter = $Filter } + +# 限制查询属性,加快查询速度 +# CIM 支持 +# WIM 不支持 +if ($isSupportCim -and $propertyList.Count -gt 0) { + $queryParams.Property = $propertyList } -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 +# 执行查询 +$results = if ($isSupportCim) { Get-Cimresult @queryParams } else { Get-WmiObject @queryParams } - # 改成 wmic 的输出格式 - if ($value -is [Array]) { - $formattedValue = ($value | ForEach-Object { "`"$_`"" }) -join "," - Write-Output "$name={$formattedValue}" - } - else { - Write-Output "$name=$value" +# 遍历结果 +foreach ($result in $results) { + # 遍历属性 + foreach ($property in $result.PSObject.Properties) { + $name = $property.Name + $value = $property.Value + + # 过滤系统属性 + if ($name.StartsWith("__") -or $name -eq "CimresultProperties" -or $name -eq "CimClass") { continue } + + # 只输出 propertyList 有的属性 + # propertyList 为空表示不过滤 + if ($propertyList.Count -eq 0 -or $propertyList -contains $name) { + + # 改成 wmic 的输出格式 + # 这里要注意 string 也是 IEnumerable + if ($value -isnot [string] -and $value -is [Collections.IEnumerable]) { + $formattedValue = ($value | ForEach-Object { "`"$_`"" }) -join "," + Write-Output "$name={$formattedValue}" + } + else { + Write-Output "$name=$value" + } } } }