Gostaria de obter toda a carga útil que retorna do PowerShell em uma única classe. Estou assumindo que Res1 e os outros devem ser tratados como uma string personalizada para colocá-la nas propriedades c#. Ainda é um trabalho em andamento...
Seria bom ter informações em aula como as seguintes:
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; }
}
Aqui está o aplicativo de console completo:
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; }
}
}
Resultados:
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"
Atualizada
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
Esperamos que isso possa ajudá-lo a simplificar seu código, não está claro o que você espera como resultado do seu código, mas o que você precisa fazer primeiro é alterar o tipo nas
string
propriedades da suaComputerInfo
classe paraPSObject
ouobject
, caso contrário, os objetos geradosInvoke-Command
serão armazenados como um simples corda. Você também pode aproveitar aInvoke<T>()
sobrecarga e deixar o PowerShell realizar a conversão dePSCustomObject
paraComputerInfo
. Estou adicionando aqui um exemplo muito simplificado do que isso significa: