简体   繁体   English

如何将变量严格键入为特定的 WMI 类?

[英]How do I strictly-type a variable as a specific WMI class?

In most cases, what you're expecting for a function parameter is [wmiclass] .在大多数情况下,您期望的函数参数是[wmiclass] However, I'm working in a custom namespace with a custom class.但是,我正在使用自定义类在自定义命名空间中工作。 When I use Get-Member , it shows the type as:当我使用Get-Member ,它将类型显示为:

System.Management.ManagementClass#ROOT\namespace\class_name

How do I specify that WMI class as a variable type?如何将该 WMI 类指定为变量类型? This example doesn't work:这个例子不起作用:

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

This returns这返回

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

For the purpose of this question, let's say I'm trying to target出于这个问题的目的,假设我正在尝试针对

ROOT\cimv2\Win32_Service

tagging c# since it's tangentially related and I'm curious if this is solved there标记c#因为它是切线相关的,我很好奇这是否在那里解决

Can you do this?你能做这个吗?

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

Or if using CIM instead of WMI, this:或者,如果使用 CIM 而不是 WMI,则:

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

TEST CASE:测试用例:

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 Test: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM