core: 支持无 wmic 下运行

This commit is contained in:
bin456789
2024-11-23 23:50:11 +08:00
parent 4305427b77
commit 823046b6bf
4 changed files with 139 additions and 18 deletions

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"
}
}
}