【问题标题】:.NET Core Powershell returns empty string when executing "Get-WmiObject".NET Core Powershell 在执行“Get-WmiObject”时返回空字符串
【发布时间】:2020-11-14 09:29:08
【问题描述】:

我正在尝试执行这个命令:

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

在 Windows 上托管的 .NET Core 3.1 应用上

这是我的代码:

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();
    }
}

但由于某种原因,它返回一个空字符串而不是例如

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

我正在使用

"Microsoft.PowerShell.SDK" 版本="7.0.3"

提前致谢

【问题讨论】:

  • 您可能必须自己读取 item 的属性,而不是假设 PowerShell 会自动返回 Format-List 输出。
  • 您还可以从 System.Management.Infrastructure(或 Microsoft.Management.Infrastructure 用于 CIM 接口而不是 WMI)获取 WMI 对象,而不是通过 PowerShell SDK。
  • @Rup 不幸的是 pipelineObjects 为空(计数 = 0)
  • 某些注册表项仅存在或只能在注册表的 32 位或 64 位分支之一中访问。使用其他版本的 Powershell 尝试相同的代码。
  • 我错了还是Get-WMIObject 在 PS v7 上根本不起作用?您是否尝试改用Get-CimInstance

标签: c# powershell asp.net-core .net-core


【解决方案1】:

我设法通过以下方式获得了这些信息:

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

foreach (var entry in data)
{
    ...
}

【讨论】:

    【解决方案2】:

    您不使用Get-CimInstance 而不是Get-WmiObject 有什么原因吗?

    如果我使用 Get-CimInstance -ClassName Win32_Product -Filter "Vendor='Microsoft'",我会在 Powershell Core 7.0.3 上得到这个结果:

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

    当我使用 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.
    

    更新:

    如果您使用不带通配符的 -Filter,则它需要一个准确的结果,这意味着如果 Vendor 的值是标准的 Microsoft Corporation 而不仅仅是 Microsoft,那么您的原始 cmdlet 不会返回任何内容。

    似乎 -Filter-Query 参数在 Get-Ciminstance 上有些不确定。

    我首先运行了一个简单的Get-CimInstance -ClassName Win32_Product,并与 Microsoft Corporation 等公司一起获得了大量结果。

    然后我两个都跑了:

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

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

    虽然他们应该支持通配符,但没有得到任何回报。

    对我有用的是:

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

    【讨论】:

    • 我仍然收到一个空字符串 / 0 个管道对象
    猜你喜欢
    • 2014-08-22
    • 2017-01-26
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2018-09-28
    • 2014-07-03
    相关资源
    最近更新 更多