简体   繁体   中英

.NET Core Powershell returns empty string when executing "Get-WmiObject"

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 eg

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

I'm using

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

Thanks in advance

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)
{
    ...
}

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*'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM