powershellwmiwinrm

Powershell Invoke-Command does not return correct output


I'm trying to collect some registry values from remote server using StdRegProv class:

Invoke-Command -ComputerName $ComputerHost -Credential $cred -ScriptBlock { Get-WmiObject -List -Namespace "root\default" | Where-Object { $_.Name -eq "StdRegProv" } }

But it doesnt return methods for these class in output:

   NameSpace: ROOT\default

Name                                Methods              Properties                                                                       PSComputerName
----                                -------              ----------                                                                       --------------
StdRegProv
                                                                                                                              

If i execute the command from ScriptBlock in machine locally, all works fine and output looks like these:

   NameSpace: ROOT\default

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

Some of other commands, like Get-Service, works ok and i can see the output. What is the problem with these one? Remote machine is: Windows Server 2003 with installed .Net 3.5.1 and PowerShell 2.0

I thought, that StdRegProv class doesnt work in 2003 server, but it works ok locally


Solution

  • As an aside:


    You're calling Get-WmiObject via PowerShell remoting (Invoke-Command -ComputerName), which for all but a handful of well-known types involves a loss of type fidelity:

    This explains why the result object is lacking methods.

    You have two options:


    CIM solution (PowerShell v3+)

    For the sake of completeness, here is the equivalent solution based on the CIM cmdlets:

    $regProv = Get-CimClass StdRegProv -CimSession (New-CimSession -ComputerName $ComputerHost -Credential $cred)
    $regProv | Invoke-CimMethod  -MethodName GetStringValue -Arguments @{ hDefKey = [Uint32] 2147483650; sSubKeyName = 'SOFTWARE\DefaultUserEnvironment'; sValueName = 'Path' }
    

    However, you can use just an Invoke-CimMethod call in this case and specify the target class via the -ClassName parameter:

    Invoke-CimMethod -ClassName StdRegProv -MethodName GetStringValue -Arguments @{ hDefKey = [Uint32] 2147483650; sSubKeyName = 'SOFTWARE\DefaultUserEnvironment'; sValueName = 'Path' }
    

    [1] These instances have an ETS (Extended Type System) type name that reflects the class and method that was invoked, e.g. Microsoft.Management.Infrastructure.CimMethodResult#StdRegProv#GetStringValue, such as reported by Get-Member.