我正在通过Cim cmdlet使用WMI API.问题是我无法弄清楚如何将wmi对象传递给接受wmi对象数组的wmi方法.
这是方法参数定义:
Name CimType Qualifiers
---- ------- ----------
Path String {ID, in}
Permissions InstanceArray {EmbeddedInstance, ID, in}
ResetChildren Boolean {ID, in}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
执行此代码将导致错误:
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
Run Code Online (Sandbox Code Playgroud)
如果你改变代码
Permissions = @($group[0]); #Trouble here
Run Code Online (Sandbox Code Playgroud)
要编码
Permissions = $group; #Trouble here
Run Code Online (Sandbox Code Playgroud)
然后错误消息也会改变:
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
Run Code Online (Sandbox Code Playgroud)
任何想法如何$group正确传递给方法?
我的方法有完全相同的问题VisualSVN_Repository::SetSecurity。
使用 CIM 方法参数时,必须将任何数组参数转换为[CimInstance[]].
例如,这对我有用:
$Everyone = Get-CimInstance -Namespace root/VisualSVN -ClassName VisualSVN_Everyone
# Grant Everyone a Read/Write access:
$AccessRule = New-CimInstance -Namespace root/VisualSVN -ClassName VisualSVN_PermissionEntry -ClientOnly -Property @{ Account = $Everyone; AccessLevel = [UInt32]2 }
$SvnRepo = Get-CimInstance -Namespace root/VisualSVN -ClassName VisualSVN_Repository -Filter "Name='MY_REPOSITORY_NAME'"
Invoke-CimMethod -InputObject $SvnRepo -MethodName SetSecurity -Arguments @{
Path = '/';
Permissions = [CimInstance[]]$AccessRule;
ResetChildren = $true
} | Out-Null
Run Code Online (Sandbox Code Playgroud)
[CimInstance[]]即使数组参数只是单个项目,也必须将其强制转换。
PS:也要小心Ref数组参数:您必须首先将其转换为[CimInstance[]],然后再转换为[ref[]]。例如调用VisualSVN_Group::Create方法时:
[CimInstance[]] $SvnUsers = [CimInstance[]]($ArrayOf_VisualSVN_User_Objects)
Invoke-CimMethod -ClassName VisualSVN_Group -Namespace root/VisualSVN -MethodName Create -Arguments @{
Members = [ref[]]$SvnUsers;
Name = 'MY_NEW_GROUP_NAME'
} | Out-Null
Run Code Online (Sandbox Code Playgroud)
另请参阅:提示#5:在 PowerShell 博客上传递引用和嵌入实例。
| 归档时间: |
|
| 查看次数: |
1605 次 |
| 最近记录: |