0

I'm trying to execute this command:

Get-WmiObject -Class Win32_Product -Filter "Vendor = 'Microsoft'"

on .NET Core 3.1 app hosted on Windows

Here's my code:

var param = new Dictionary<string, object>();
param.Add("Class", "Win32_Product");
param.Add("Filter", "Vendor = 'Microsoft'");

var result = await ScriptHelper.RunScript("Get-WmiObject", param);


public static async Task<string> RunScript(string scriptContents, Dictionary<string, object> scriptParameters)
{
    using (PowerShell ps = PowerShell.Create())
    {
        ps.AddScript(scriptContents);
        ps.AddParameters(scriptParameters);

        var pipelineObjects = await ps.InvokeAsync().ConfigureAwait(false);
        
        var sb = new StringBuilder();
        
        foreach (var item in pipelineObjects)
        {
            sb.AppendLine(item.BaseObject.ToString());
        }

        return sb.ToString();
    }
}

But for some reason it returns an empty string instead of e.g

IdentifyingNumber : ...
Name              : ...
Vendor            : Microsoft
Version           : ...
Caption           : ...

I'm using

"Microsoft.PowerShell.SDK" Version="7.0.3"

Thanks in advance

7
  • You might have to read the properties from item yourself, rather than assuming PowerShell will return you the Format-List output automatically. Commented Jul 24, 2020 at 13:47
  • You can also get WMI objects from System.Management.Infrastructure (or Microsoft.Management.Infrastructure for the CIM interface not WMI) rather than going through the PowerShell SDK. Commented Jul 24, 2020 at 13:49
  • @Rup Unfortunely pipelineObjects is Empty (count = 0) Commented Jul 24, 2020 at 13:50
  • Some registry keys only exist or are only accessible in one of the 32 bits or 64 bits branches of the registry. Try the same code with the other version of Powershell. Commented Jul 24, 2020 at 13:53
  • 1
    Am I wrong or does Get-WMIObject not work on PS v7 at all? Did you try to use Get-CimInstance instead? Commented Jul 24, 2020 at 13:58

2 Answers 2

1

I managed to obtain those informations via:

using System.Management;
var data = new ManagementObjectSearcher("SELECT * FROM Win32_Product WHERE Vendor = 'Microsoft'").Get();

foreach (var entry in data)
{
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Is there a reason you're not using Get-CimInstance rather than Get-WmiObject?

If I use Get-CimInstance -ClassName Win32_Product -Filter "Vendor='Microsoft'" I get this result on Powershell Core 7.0.3:

Name             Caption                        Vendor                         Version                        IdentifyingNumber
----             -------                        ------                         -------                        -----------------
PowerToys (Prev… PowerToys (Preview)            Microsoft                      0.19.2                         {3EFDE709-F7B5-4AC9-8263-80D… 

while I get the following error message using Get-WmiObject:

Get-WmiObject: The term 'Get-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Update:

If you use -Filter without wildcards it expects an exact result which means your original cmdlet does not return anything if the value of Vendor is the standard Microsoft Corporation rather than just Microsoft.

It seems that the -Filter and -Query parameters are somewhat iffy on Get-Ciminstance.

I first ran a simple Get-CimInstance -ClassName Win32_Product and got back a ton of results with among others Microsoft Corporation.

Then I ran both:

Get-CimInstance -ClassName Win32_Product -Filter "Vendor like 'Microsoft*'"

and

Get-CimInstance -Query "SELECT * FROM Win32_Product WHERE Vendor LIKE 'Microsoft*'"

And got nothing back though they should support wild cards.

What worked for me was this:

Get-CimInstance -ClassName Win32_Product | Where-Object {$_.Vendor -like 'Microsoft*'}

1 Comment

I'm still receiving an empty string / 0 pipelined objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.