Как мне строго ввести переменную как определенный класс WMI? - PullRequest
1 голос
/ 08 марта 2019

В большинстве случаев вы ожидаете получить параметр функции [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#, поскольку она имеет тангенциальное отношение, и мне любопытно, если это там будет решено

1 Ответ

1 голос
/ 08 марта 2019

Вы можете сделать это?

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

ДЕЛО ТЕСТА:

function test {

param([psTypename("System.Management.ManagementClass#ROOT\cimv2\StdRegProv")]$mine)
$mine
}

$reg = [wmiclass]"\\.\root\cimv2:StdRegprov"
$reg | gm

   TypeName: System.Management.ManagementClass#ROOT\cimv2\StdRegProv

[wmiclass]$wmi = ""
$wmi | gm

   TypeName: System.Management.ManagementClass#\

test $wmi
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

   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

   NameSpace: ROOT\cimv2

Name                                Methods              Properties
----                                -------              ----------
StdRegProv                          {CreateKey, Delet... {}

testv2 $wmi

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...