【发布时间】:2015-03-17 05:45:10
【问题描述】:
我正在通过 Cim cmdlet 使用 WMI API。问题是我不知道如何将 wmi 对象传递给接受 wmi 对象数组的 wmi 方法。
这里是方法参数定义:
Name CimType Qualifiers
---- ------- ----------
Path String {ID, in}
Permissions InstanceArray {EmbeddedInstance, ID, in}
ResetChildren Boolean {ID, in}
Path 和 ResetChildren 是简单的参数。它们分别接受像"/path" 和$true 这样的简单值。但是Permissions参数有问题。
这是我的代码
#Acquiring object that I want to pass to method
$group = Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Group -Filter "Name='Readers'"
#Acquiring object which method will be called
$repositories = Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Repository
#Preparing method arguments
$args = @{
Path = "/";
Permissions = @($group[0]); #Trouble here
ResetChildren = $true
}
#Invoking method with arguments
Invoke-CimMethod -InputObject ($repositories[0]) -MethodName SetSecurity -Arguments $args
执行这段代码会报错:
Invoke-CimMethod : Unable to cast object of type 'Microsoft.Management.Infrastructure.CimInstance' to type 'M
icrosoft.Management.Infrastructure.Native.InstanceHandle'.
Parameter name: value
At C:\somepath\script1.ps1:11 char:1
+ Invoke-CimMethod -InputObject ($repositories[0]) -MethodName SetSecurity -Argume ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-CimMethod], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.Management.Infrastructure.CimCmdlets.Invoke
CimMethodCommand
如果您更改代码
Permissions = @($group[0]); #Trouble here
编码
Permissions = $group; #Trouble here
那么错误信息也会改变:
Invoke-CimMethod : Unable to cast object of type 'Microsoft.Management.Infrastructure.Native.InstanceHandle'
to type 'System.Collections.IList'.
Parameter name: value
At C:\somepath\script1.ps1:11 char:1
+ Invoke-CimMethod -InputObject ($repositories[0]) -MethodName SetSecurity -Argume ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-CimMethod], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.Management.Infrastructure.CimCmdlets.Invoke
CimMethodCommand
任何想法如何将$group 正确传递给方法?
【问题讨论】:
-
我没有安装 VisualSVN,所以无法测试,但是你的“$group[0]”对象有 Handle 属性吗?如果是,那么传递“$group[0].Handle”怎么样?
-
我认为这个麻烦与VisualSVN没有直接关系。因此,您可以使用任何具有相似签名的 WMI 方法来重现它。方法必须接受一些 WMI 对象的数组。不幸的是,我的“$group[0]”既没有 Handle 属性,也没有与 InstanceHandle 相关的东西。 InstanceHandle 类与内部实现细节相关,没有任何公共文档。我猜想当 PowerShell 做一些魔术将 PowerShell 内部 WMI 对象表示转换为纯 WMI 对象时,会抛出异常。
-
我明白这不是 VisualSVN 问题,重现错误就好了。这是另一个示例 [链接]powershell.com/cs/forums/p/13888/26108.aspx。不幸的是,我也无法复制它。我发现的一个“提示”是“CIM cmdlet 返回惰性对象”([链接]blogs.msmvps.com/richardsiddaway/category/powershellandwmi/page/…)它解释了 get-cmistance 返回的对象是没有任何方法的 WMI 对象的“惰性”副本。我会尝试使用 Get-WmiObject 重写代码,看看是否可以直接调用该方法。
-
我编写了脚本,用于在计算机中搜索所有接受对象数组的 WMI 方法。 Get-Methods.ps1。希望这可以帮助您重现情况。
标签: powershell wmi