Pass an array of CimInstance to CimMethod

I work with the WMI API through Cim cmdlets. The problem is that I cannot figure out how to pass a wmi object to a wmi method that accepts an array of wmi objects.

Here is the definition of the method parameters:

Name CimType Qualifiers ---- ------- ---------- Path String {ID, in} Permissions InstanceArray {EmbeddedInstance, ID, in} ResetChildren Boolean {ID, in} 

Path and ResetChildren are simple parameters. They take simple values, such as "/path" and $true respectively. But I have problems with the Permissions parameter.

Here is my code

 #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 

Executing this code will result in an error:

 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 

If you change the code

 Permissions = @($group[0]); #Trouble here 

The code

 Permissions = $group; #Trouble here 

Then the error message will also change:

 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 

Any ideas on how to pass the $group method correctly?

+5
source share
3 answers

I had exactly the same problem with the VisualSVN_Repository::SetSecurity .

When working with arguments to the CIM method, you must specify the ANY arguments of the array to [CimInstance[]] .

For example, this worked for me:

 $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 

You must specify the [CimInstance[]] array argument, even if it is just one element.

PS: Be careful with the arguments of the Ref : array, you must first transfer it to [CimInstance[]] and then to [ref[]] . For example, when calling the VisualSVN_Group::Create method:

 [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 

See also: Tip # 5: Share links and embedded instances on the PowerShell Blog.

+1
source

If you had reprograms that did not rely on software that I did not install, I could answer completely, so I will do my best based on what I can do from your question.

To do what you assign Permissions array and you assign only one value, use a comma in front.

Permissions = ,$group

Here is a sample script and output for demonstration.

Script

 $var1 = "any value" $var2 = ,$var1 $var1.GetType() $var2.GetType() $var2.Count $var2[0] 

Exit

 IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object True True Object[] System.Array 1 any value 
0
source

A possible solution (it worked for me with a similar problem that brought me here) based on a "hint" that CIM are inert objects. In my case, he installed the SCCM updates with a script, allowing to restart if it required updating, and stopped if the whole process took more than a certain time (so as not to go through the available maintenance window).

I passed the CIMUpdate object through the powershell function to perform an update that allows you to restart the updates if necessary (perform one update at a time, check for a restart, then try the next update).

The CIM object threw a "Type Mismatch" error when it was forced to run as [CIMInstance[]] , and if casting was not performed at all, I would get the same error message Unable to cast object of type 'Microsoft.Management.Infrastructure.CimInstance' to type 'Microsoft.Management.Infrastructure.Native.InstanceHandle' .

The solution was to use this CIMUpdate and re-query to get the version of the live object inside the function, and then use this new object as an argument to CIMMethod.

0
source

Source: https://habr.com/ru/post/1211395/


All Articles