Я предполагаю, что мне нужно сделать что -то вручную с Res1 (Hashtable PowerShell в форме строки)C#

Место общения программистов C#
Ответить
Anonymous
 Я предполагаю, что мне нужно сделать что -то вручную с Res1 (Hashtable PowerShell в форме строки)

Сообщение Anonymous »

Я хотел бы получить всю полезную нагрузку, которая возвращается от PowerShell в один класс. Я предполагаю, что RES1, а другие должны рассматриваться как индивидуальная строка, чтобы получить это в свойства C#. Это все еще в процессе работы ... < /p>
Было бы неплохо иметь информацию в классе, например, следующее: < /p>
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 FolderGroup1Names { get; set; }
public List FolderGroup2Names { get; set; }
}
< /code>
Вот все приложение консоли: < /p>
using System.Collections.ObjectModel;
using System.Management.Automation;

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

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
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; }
}
}
< /code>
Результаты: < /p>
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"

обновлен
computers[0].Res1
{@{Used=130976595968; Free=136249831424}}
BaseObject: "@{Used=130976595968; Free=136249831424}"
ImmediateBaseObject: "@{Used=130976595968; Free=136249831424}"
Members: {System.Management.Automation.PSMemberInfoIntegratingCollection}
Methods: {System.Management.Automation.PSMemberInfoIntegratingCollection}
Properties: {System.Management.Automation.PSMemberInfoIntegratingCollection}
TypeNames: Count = 2
Dynamic View: Expanding the Dynamic View will get the dynamic members for the object
< /code>
< /p>

Это то, что у меня есть, и я не получу никаких записей, но если я вставлю в Powershell, я получаю записи < /p>
я не получу никаких записей, но если я вставлю в Powershell, я получу записи.....
....
....
Invoke-Command -ComputerName $computers {
Get-Service -Name *ssp* | Select-Object Name, Status, PSComputerName, StartType
}
")
.Invoke()
.ToArray();
}
}
}

public class SspService
{
public string? Name { get; set; }
public string? Status { get; set; }
public string? PSComputerName { get; set; }
public string? StartType { get; set; }

}


Подробнее здесь: https://stackoverflow.com/questions/774 ... shtable-in
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»