首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何严格地将变量键入为特定的WMI类?

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

Stack Overflow用户
提问于 2019-03-07 21:01:37
回答 1查看 224关注 0票数 2

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

代码语言:javascript
运行
复制
System.Management.ManagementClass#ROOT\namespace\class_name

如何将WMI类指定为变量类型?此示例不起作用:

代码语言:javascript
运行
复制
param(
    [wmiclass#root\namespace\class_name]
    $Class
)

这会返回

代码语言:javascript
运行
复制
Unable to find type [System.Management.ManagementClass#ROOT\namespace\class_name].

就这个问题来说,假设我正试图瞄准

代码语言:javascript
运行
复制
ROOT\cimv2\Win32_Service

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-07 21:40:54

你能做到吗?

代码语言:javascript
运行
复制
Param (
    [PsTypeName("System.Management.ManagementClass#ROOT\namespace\class_name")]
    $Class
)

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

代码语言:javascript
运行
复制
Param (
    [PsTypeName("System.Management.Infrastructure.CimInstance#root/namespace/class_name")]
    $Class
)

测试用例:

代码语言:javascript
运行
复制
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测试:

代码语言:javascript
运行
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55052723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档