小编mfr*_*ree的帖子

如何在C#中获取PSObject.Properties的ScriptProperty值?

我正在尝试使用“ GET-PSDrive”命令通过PowerShell 6.0获取服务器的驱动器信息。直接在PowerShell中运行命令,我在输出表中看到“ Used”和“ Free”的值,但是使用Microsoft.Powershell.Sdk在代码中运行相同的命令,但“ Used”和“ Free”字段却没有可用。

我在PSObject.Properties数组下看到了两个项目,但是尝试访问该值时会收到异常:

“此线程中没有可用于运行脚本的运行空间。您可以在System.Management.Automation.Runspaces.Runspace类型的DefaultRunspace属性中提供一个。您尝试调用的脚本块是:##”

以下是我正在使用的POC代码:

using (var psCon = PowerShell.Create())
{
     psCon.AddCommand("GET-PSDrive");
     var psReturn = psCon.Invoke();
     foreach (var psObj in psReturn)
     {
          var driveUsedValue = psObj.Properties["Used"].Value;
     }
}
Run Code Online (Sandbox Code Playgroud)

我期望获得该属性的值,但是每次评估该值时,我都会收到一条错误消息,指出没有可用的运行空间。检查该属性,我确实看到它是一个ScriptProperty,那么您如何获得该生成的值?

c# powershell powershell-sdk .net-core

4
推荐指数
1
解决办法
226
查看次数

标签 统计

.net-core ×1

c# ×1

powershell ×1

powershell-sdk ×1