c# - 如何将变量严格键入为特定 WMI 类?

标签 c# .net powershell wmi

在大多数情况下,您期望的函数参数是[wmiclass]。但是,我正在使用自定义类在自定义命名空间中工作。当我使用 Get-Member 时,它显示的类型为:

System.Management.ManagementClass#ROOT\namespace\class_name

如何将该 WMI 类指定为变量类型?这个例子不起作用:

param(
    [wmiclass#root\namespace\class_name]
    $Class
)

这会返回

Unable to find type [System.Management.ManagementClass#ROOT\namespace\class_name].

出于这个问题的目的,假设我正在尝试定位

ROOT\cimv2\Win32_Service

标记c#,因为它是切向相关的,我很好奇这个问题是否在那里得到解决

最佳答案

你能做到吗?

Param (
    [PsTypeName("System.Management.ManagementClass#ROOT\namespace\class_name")]
    $Class
)

或者如果使用 CIM 而不是 WMI,则:

Param (
    [PsTypeName("System.Management.Infrastructure.CimInstance#root/namespace/class_name")]
    $Class
)

测试用例:

function test {
    Param (
        [psTypename("System.Management.ManagementClass#ROOT\cimv2\StdRegProv")]
        $mine
    )
    $mine
}

$reg = [wmiclass]"\\.\root\cimv2:StdRegprov"
$reg | gm
#outputs:    TypeName: System.Management.ManagementClass#ROOT\cimv2\StdRegProv

[wmiclass]$wmi = ""
$wmi | gm
# outputs:    TypeName: System.Management.ManagementClass#\

test $wmi
# Errors:    test : Cannot bind argument to parameter 'mine', because PSTypeNames of the argument do not match the PSTypeName
# required by the parameter: System.Management.ManagementClass#ROOT\cimv2\StdRegProv.
# At line:1 char:6
# + test $wmi
# +      ~~~~
#     + CategoryInfo          : InvalidArgument: (:) [test], ParameterBindingArgumentTransformationException
#     + FullyQualifiedErrorId : MismatchedPSTypeName,test

test $reg
# outputs:    NameSpace: ROOT\cimv2
# Name                                Methods              Properties
# ----                                -------              ----------
# StdRegProv                          {CreateKey, Delet... {}

PowerShell V2 测试:

function testv2 {    
    param(
        [ValidateScript({($_ | Get-Member)[0].typename -eq 'System.Management.ManagementClass#ROOT\cimv2\StdRegProv'})]
        $mine
    )
    $mine
}

testv2 $reg

# outputs:    NameSpace: ROOT\cimv2
#
# Name                                Methods              Properties
# ----                                -------              ----------
# StdRegProv                          {CreateKey, Delet... {}

testv2 $wmi

# Error:    testv2 : Cannot validate argument on parameter 'mine'. The "($_ | gm)[0].typename -eq 'System.Management.ManagementClas
# s#ROOT\cimv2\StdRegProv'" validation script for the argument with value "" did not return true. Determine why the valid
# ation script failed and then try the command again.
# At line:1 char:7
# + testv2 <<<<  $wmi
#     + CategoryInfo          : InvalidData: (:) [testv2], ParameterBindingValidationException
#     + FullyQualifiedErrorId : ParameterArgumentValidationError,testv2

关于c# - 如何将变量严格键入为特定 WMI 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55052723/

相关文章:

c# - 在asp.net c# 中禁用RequiredFieldValidator

.net - 64 位 MSBuild 的优势?

powershell - 'Install-Module' : is not recognized as the name of a cmdlet, 函数、脚本文件或可运行程序运行时出错

events - 我可以在 Microsoft Lync API 中的何处设置传入 IM 的事件处理程序?

powershell - 从数组列表中删除一个 hastable

c# - 使用 Navmesh 和协程的基本 RTS 运动?

c# - 搜索大量大文本列表的最快方法

c# - 奇怪的字符渲染

.net - 哪种编程语言最容易(对于用户)用作集成脚本语言?

基于每个文件的 C# 编译器常量?