猿问

如何将变量严格键入为特定的 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#,因为它是切线相关的,我很好奇这是否在那里解决


ITMISS
浏览 114回答 1
1回答

明月笑刀无情

你能做这个吗?Param (&nbsp; &nbsp; [PsTypeName("System.Management.ManagementClass#ROOT\namespace\class_name")]&nbsp; &nbsp; $Class)或者,如果使用 CIM 而不是 WMI,则:Param (&nbsp; &nbsp; [PsTypeName("System.Management.Infrastructure.CimInstance#root/namespace/class_name")]&nbsp; &nbsp; $Class)测试用例:function test {&nbsp; &nbsp; Param (&nbsp; &nbsp; &nbsp; &nbsp; [psTypename("System.Management.ManagementClass#ROOT\cimv2\StdRegProv")]&nbsp; &nbsp; &nbsp; &nbsp; $mine&nbsp; &nbsp; )&nbsp; &nbsp; $mine}$reg = [wmiclass]"\\.\root\cimv2:StdRegprov"$reg | gm#outputs:&nbsp; &nbsp; TypeName: System.Management.ManagementClass#ROOT\cimv2\StdRegProv[wmiclass]$wmi = ""$wmi | gm# outputs:&nbsp; &nbsp; TypeName: System.Management.ManagementClass#\test $wmi# Errors:&nbsp; &nbsp; 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# +&nbsp; &nbsp; &nbsp; ~~~~#&nbsp; &nbsp; &nbsp;+ CategoryInfo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : InvalidArgument: (:) [test], ParameterBindingArgumentTransformationException#&nbsp; &nbsp; &nbsp;+ FullyQualifiedErrorId : MismatchedPSTypeName,testtest $reg# outputs:&nbsp; &nbsp; NameSpace: ROOT\cimv2# Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Methods&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Properties# ----&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ----------# StdRegProv&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {CreateKey, Delet... {}PowerShell V2 测试:function testv2 {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; param(&nbsp; &nbsp; &nbsp; &nbsp; [ValidateScript({($_ | Get-Member)[0].typename -eq 'System.Management.ManagementClass#ROOT\cimv2\StdRegProv'})]&nbsp; &nbsp; &nbsp; &nbsp; $mine&nbsp; &nbsp; )&nbsp; &nbsp; $mine}testv2 $reg# outputs:&nbsp; &nbsp; NameSpace: ROOT\cimv2## Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Methods&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Properties# ----&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ----------# StdRegProv&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {CreateKey, Delet... {}testv2 $wmi# Error:&nbsp; &nbsp; 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 <<<<&nbsp; $wmi#&nbsp; &nbsp; &nbsp;+ CategoryInfo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : InvalidData: (:) [testv2], ParameterBindingValidationException#&nbsp; &nbsp; &nbsp;+ FullyQualifiedErrorId : ParameterArgumentValidationError,testv2
随时随地看视频慕课网APP
我要回答