【问题标题】:Why can't i return all cimsession functions from Powershell.Invoke() method in c#为什么我不能从 C# 中的 Powershell.Invoke() 方法返回所有 cimsession 函数
【发布时间】:2020-05-19 04:31:56
【问题描述】:

我正在从 Powershell 和 C# 运行 New-CimSession 命令。当我在 Powershell 上运行它时,CimSession 变量有很多函数,如TestConnection()CreateInstance 等。图片如下。

但是,当我在c#中从Powershell.Invoke()方法运行New-CimSession命令时,PSObjectCimSession变量类型除了ToString()GetType()之外没有其他功能。

我想从 c# 上的 PSObject 变量访问相同的方法。 你有什么想法吗?

【问题讨论】:

  • 嗨 Emre,如果解决方案成功了,请告诉我

标签: c# powershell psobject


【解决方案1】:
  • 添加对Microsoft.Management.Infrastructure Namespace的引用
  • 添加`使用 Microsoft.Management.Infrastructure;
static void Main(string [] args)
{
     var csession = CimSession.Create("");
     csession.TestConnection();
}

csession 将可以访问您想要的不同方法

参考这里:

  • “C:\Program Files (x86)\Reference Assemblies\Microsoft\WMI\v1.0\Microsoft.Management.Infrastructure.dll”

  • C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Management.Infrastructure

【讨论】:

  • 是的,你是对的。这种方式有帮助,但我的项目是 .NET Framework 4.8。我接受了这个错误。 “无法安装包 'Microsoft.Management.Infrastructure.Runtime.Unix 2.0.0'。您正在尝试将此包安装到以 '.NETFramework,Version=v4.8' 为目标的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件。有关详细信息,请联系包作者。"
  • 是的,我尝试使用 Nuget,但我查看 .dll 但没有 C:\Program Files (x86)\Reference Assemblies\Microsoft\WMI\v1。 0"
  • 是的,Windows 10
  • 真的谢谢你。我在那里找到了。现在,我将尝试将其实施到项目中。
  • 当然好,如果可行,您可以接受我的回答!!
猜你喜欢
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 2019-01-08
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
相关资源
最近更新 更多