我希望将从PowerShell返回的所有有效负载放到一个类中.我假设res1和其他字符串必须作为自定义字符串处理,以便将其放入c#属性.它仍然是一项正在进行的工作……

如果课堂上有这样的信息就好了:

MyClass{
  public string ComputerName { get; set; }
  public string CUsedSpace { get; set; }
  public string CFreeSpace { get; set; }
  public string DUsedSpace { get; set; }
  public string DFreeSpace { get; set; }
  public List<string>  FolderGroup1Names { get; set; }
  public List<string>  FolderGroup2Names { get; set; }
}

以下是整个控制台应用程序:

using System.Collections.ObjectModel;
using System.Management.Automation;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<ComputerInfo> computers = new List<ComputerInfo>();

            using (PowerShell ps = PowerShell.Create())
            {
                ps.AddScript(@"
$computers = 
@""
Server1
""@ -split [Environment]::NewLine

$oRes1 = Invoke-Command -ComputerName $computers {

    $test = 
    @{ 
        res1 = Get-PSDrive C | Select-Object Used,Free;
        res2 = Get-PSDrive D | Select-Object Used,Free;
        res3 = hostname;
        res4 = Get-ChildItem ""c:\temp"";
        res5 = Get-ChildItem ""d:\temp""
    }

    $test
}

[PSCustomObject]$oRes1

");

                Collection<PSObject> result = ps.Invoke();
                foreach (var outputObject in result)
                {
                    computers.Add(new ComputerInfo()
                    {
                        Res1 = $"{outputObject.Properties["Res1"].Value}",
                        Res2 = $"{outputObject.Properties["Res2"].Value}",
                        Res3 = $"{outputObject.Properties["Res3"].Value}",
                        Res4 = $"{outputObject.Properties["Res4"].Value}",
                        Res5 = $"{outputObject.Properties["Res5"].Value}"
                    });
                }
            }
        }
    }

    class ComputerInfo
    {
        public string Res1 { get; set; }
        public string Res2 { get; set; }
        public string Res3 { get; set; }
        public string Res4 { get; set; }
        public string Res5 { get; set; }
    }
}

结果:

ConsoleApp1.ComputerInfo,"@{Used=130977062912; Free=136249364480}","@{Used=130977062912; Free=136249364480}",Server1,"2023-06-13 2023-06-20 2023-06-21 2023-06-22 2023-06-23 2023-06-24 2023-06-26 2023-06-27 2023-06-28 2023-06-29 2023-06-30 2023-07-01 2023-07-03 2023-07-04 2023-07-05 2023-07-06 2023-07-07 2023-07-08 2023-07-10 2023-07-11 2023-07-12 2023-07-13 2023-07-14 2023-07-15 2023-07-17 2023-07-18 2023-07-19 2023-07-20 2023-07-21 2023-07-22 2023-07-24 2023-07-25 2023-07-26 2023-07-27 2023-07-28 2023-07-29 2023-07-31 2023-08-01 2023-08-02 2023-08-03 2023-08-04 2023-08-05 2023-08-07 2023-08-08 2023-08-09 2023-08-10 2023-08-11 2023-08-12 2023-08-14 2023-08-15 2023-08-16 2023-08-17 2023-08-18 2023-08-19 2023-08-21 2023-08-22 2023-08-23 2023-08-24 2023-08-25 2023-08-26 2023-08-28 2023-08-29 2023-08-30 2023-08-31 2023-09-01 2023-09-02 2023-09-04 2023-09-05 2023-09-06 2023-09-07 2023-09-08 2023-09-09 2023-09-11 2023-09-12 2023-09-13 2023-09-14 2023-09-15 2023-09-16 2023-09-18 2023-09-19 2023-09-20 2023-09-21 2023-09-22 2023-09-23 2023-09-25 2023-09-26 2023-09-27 2023-09-28 2023-09-29 2023-09-30 2023-10-02 2023-10-03 2023-10-04 2023-10-05 2023-10-06 2023-10-07 2023-10-09 2023-10-10 2023-10-11 2023-10-12 2023-10-13 2023-10-14 2023-10-16 2023-10-17 2023-10-18 2023-10-19 2023-10-20 2023-10-21 2023-10-23 2023-10-24 2023-10-25 2023-10-26 2023-10-27 2023-10-28 2023-10-30 2023-10-31 2023-11-01 2023-11-02 2023-11-03 2023-11-04 2023-11-06 2023-11-07 2023-11-08 2023-11-09 2023-11-10 2023-11-11 2023-11-13","2023-10-28 2023-10-30 2023-10-31 2023-11-01 2023-11-02 2023-11-03 2023-11-04 2023-11-06 2023-11-07 2023-11-08 2023-11-09 2023-11-10 2023-11-11 2023-11-13"

Updated

computers[0].Res1
{@{Used=130976595968; Free=136249831424}}
    BaseObject: "@{Used=130976595968; Free=136249831424}"
    ImmediateBaseObject: "@{Used=130976595968; Free=136249831424}"
    Members: {System.Management.Automation.PSMemberInfoIntegratingCollection<System.Management.Automation.PSMemberInfo>}
    Methods: {System.Management.Automation.PSMemberInfoIntegratingCollection<System.Management.Automation.PSMethodInfo>}
    Properties: {System.Management.Automation.PSMemberInfoIntegratingCollection<System.Management.Automation.PSPropertyInfo>}
    TypeNames: Count = 2
    Dynamic View: Expanding the Dynamic View will get the dynamic members for the object

enter image description here

推荐答案

希望这能帮助您简化代码,目前还不清楚您希望从代码中得到什么结果,但您首先需要做的是将ComputerInfo类的属性中的string类型更改为PSObjectobject,否则从Invoke-Command输出的那些对象将存储为简单的字符串.

您还可以利用Invoke<T>()重载,让PowerShell执行从PSCustomObjectComputerInfo的转换.我在这里添加一个非常简单的例子,说明这意味着什么:

Add-Type @'
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

namespace ConsoleApp1
{
    public class ComputerInfo
    {
        public string ComputerName { get; set; }
        public string CUsedSpace { get; set; }
        public string CFreeSpace { get; set; }
        public string DUsedSpace { get; set; }
        public string DFreeSpace { get; set; }
        public List<string> FolderGroup1Names { get; set; }
        public List<string> FolderGroup2Names { get; set; }
    }

    public class Program
    {
        public static ComputerInfo[] Test()
        {
            using (PowerShell ps = PowerShell.Create())
            {
                return ps.AddScript(@"
                    # NOTE: removed -ComputerName here for simplicity
                    Invoke-Command {
                        $psdC = Get-PSDrive C
                        $psdD = Get-PSDrive D

                        @{
                            ComputerName      = $env:COMPUTERNAME
                            CUsedSpace        = $psdC.Used
                            CFreeSpace        = $psdC.Free
                            DUsedSpace        = $psdD.Used
                            DFreeSpace        = $psdD.Free
                            FolderGroup1Names = (Get-ChildItem c:\).Name
                            FolderGroup2Names = (Get-ChildItem d:\).Name
                        }
                    }
                ")
                .Invoke<ComputerInfo>()
                .ToArray();
            }
        }
    }
}
'@ -WarningAction 0 -IgnoreWarnings

$result = [ConsoleApp1.Program]::Test()
$result[0].GetType()

#    Namespace: ConsoleApp1
#
# Access        Modifiers           Name
# ------        ---------           ----
# public        class               ComputerInfo : object

$result.FolderGroup1Names  # works
$result.ComputerName       # works

Csharp相关问答推荐

O(N)测试失败

有没有一种方法可以在包含混合文本的标签中嵌入超链接?

ASP.NET核心结果.文件vs结果.流

将列表字符串映射为逗号分隔字符串<>

WPF Windows初始化正在锁定. Net 8中分离的线程

使用LayoutKind在C#中嵌套 struct .显式

使用HttpResponseMessage中的嵌套列表初始化JSON

Docker Container中的HttpRequest后地址不可用

C#Null判断处理失败

如何使用.NET6WPF打印车票?

正在try 从Blazor中的API读取JSON

Regex字母数字校验

如何在Akka.NET中重新启动执行元时清除邮箱

Azure Functions v4中的Serilog控制台主题

如何从Azure函数使用Graph API(SDK 5.35)中的[FindMeetingTimes]

如何在Polly重试策略成功之前将HttpClient请求排队?

如何在使用Google.Drive.apis.V3下载文件/文件夹之前压缩?

如何从原始图像到新创建的图像获得相同的特定 colored颜色 ,并且具有相同的 colored颜色 量和相同的宽度和高度?

仅在Blazor Web App中覆盖生产的基本路径(.NET8中的_Hosts.cshtml文件功能?)

初始化具有EntityFrameworkCore和不同架构的数据库时引发System.MissingMethodExcept