core: 优化 powershell 查询

- 避免 wmic where 条件有空格时报错
- 减少 ForEach-Object 以提高速度
This commit is contained in:
bin456789
2026-04-25 23:16:53 +08:00
parent 6341c21d0b
commit e2a8be7bb7
2 changed files with 48 additions and 26 deletions

View File

@ -643,13 +643,12 @@ wmic() {
curl -Lo "$tmp/wmic.ps1" "$confhome/wmic.ps1" curl -Lo "$tmp/wmic.ps1" "$confhome/wmic.ps1"
fi fi
# shellcheck disable=SC2046
powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass \ powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass \
-File "$(cygpath -w "$tmp/wmic.ps1")" \ -File "$(cygpath -w "$tmp/wmic.ps1")" \
-Namespace "$namespace" \ -Namespace "$namespace" \
-Class "$class" \ -Class "$class" \
$([ -n "$filter" ] && echo -Filter "$filter") \ ${filter:+"-Filter"} ${filter:+"$filter"} \
$([ -n "$props" ] && echo -Properties "$props") ${props:+"-Properties"} ${props:+"$props"}
} }
is_virt() { is_virt() {

View File

@ -1,36 +1,59 @@
param( param(
[string]$Namespace, [string]$Namespace = "root\cimv2",
[string]$Class, [Parameter(Mandatory = $true)] [string]$Class,
[string]$Filter, [string]$Filter,
[string]$Properties [string]$Properties
) )
$propertiesToDisplay = if ($Properties) { $Properties.Split(",") } else { @("*") } # 预处理属性列表:如果有输入则处理成数组,否则保持为空数组
[string[]]$propertyList = if ($Properties) {
$wmiQuery = @{ $Properties.Split(",") | ForEach-Object { $_.Trim() }
Namespace = $Namespace }
Class = $Class else {
@()
} }
if ($Filter) { # 检查是否支持 Get-Cimresult
$wmiQuery.Filter = $Filter $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 { $results = if ($isSupportCim) { Get-Cimresult @queryParams } else { Get-WmiObject @queryParams }
-not $_.Name.StartsWith("__") -and
($propertiesToDisplay -contains $_.Name -or $propertiesToDisplay -contains "*")
} | ForEach-Object {
$name = $_.Name
$value = $_.Value
# 改成 wmic 的输出格式 # 遍历结果
if ($value -is [Array]) { foreach ($result in $results) {
$formattedValue = ($value | ForEach-Object { "`"$_`"" }) -join "," # 遍历属性
Write-Output "$name={$formattedValue}" foreach ($property in $result.PSObject.Properties) {
} $name = $property.Name
else { $value = $property.Value
Write-Output "$name=$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"
}
} }
} }
} }