Как я могу использовать Where-Object в PSCommand в С#?C#

Место общения программистов C#
Ответить
Anonymous
 Как я могу использовать Where-Object в PSCommand в С#?

Сообщение Anonymous »

Я создаю несколько условных операторов, которые рассматривают различные команды PowerShell. У меня возникла проблема с get-hotfix, где я хочу использоватьwhere-object, но я получаю эту ошибку при вызове PSCommand

System.Management.Automation.RemoteException: «Невозможно привязать параметр FilterScript». Невозможно преобразовать значение «$_.InstalledOn -gt $StartTimeGenerated» типа «System.String» в тип «System.Management.Automation.ScriptBlock».

Вот мой код:
if(command == "get-hotfix")
{
_HotFixViewAll = ReadInfo("ViewAllHotFix");
_hotfixInterval = Convert.ToDecimal(ReadInfo("HotFixInterval"));

if (_HotFixViewAll != "Y")
{
DateTime DateNow = DateTime.Now;
TimeSpan datetimeInterval = TimeSpan.FromDays((double)_hotfixInterval);
DateTime StartTimeGenerated = DateNow.Subtract(datetimeInterval);

var filterScript = ScriptBlock.Create("$_.InstalledOn -gt $StartTimeGenerated");
var whereCommand = new Command("Where-Object");
whereCommand.Parameters.Add("FilterScript", filterScript);
whereCommand.Parameters.Add("ArgumentList", StartTimeGenerated);
pipeline.Commands.Add(whereCommand);
}

var sortCommand = new Command("Sort-Object");
sortCommand.Parameters.Add("Property", "InstalledOn");
sortCommand.Parameters.Add("Descending");
pipeline.Commands.Add(sortCommand);
}

Как решить эту проблему?
Полный подход
using (var runspace = RunspaceFactory.CreateRunspace(connectioninfo))
{
runspace.Open();
using (var pipeline = runspace.CreatePipeline())
{
// Command to execute remotely
var psCommand = new Command(command);
pipeline.Commands.Add(psCommand);

if(command == "get-hotfix")
{
_HotFixViewAll = ReadInfo("ViewAllHotFix");
_hotfixInterval = Convert.ToDecimal(ReadInfo("HotFixInterval"));

if (_HotFixViewAll != "Y")
{
DateTime DateNow = DateTime.Now;
TimeSpan datetimeInterval = TimeSpan.FromDays((double)_hotfixInterval);
DateTime StartTimeGenerated = DateNow.Subtract(datetimeInterval);

var filterScript = ScriptBlock.Create("$_.InstalledOn -gt $StartTimeGenerated");
var whereCommand = new Command("Where-Object");
whereCommand.Parameters.Add("FilterScript", filterScript);
whereCommand.Parameters.Add("ArgumentList", StartTimeGenerated);
pipeline.Commands.Add(whereCommand);
}

var sortCommand = new Command("Sort-Object");
sortCommand.Parameters.Add("Property", "InstalledOn"); // Sort by InstalledOn column
sortCommand.Parameters.Add("Descending"); // Sort in descending order
pipeline.Commands.Add(sortCommand);
}
else if (command == "Get-ADDomainController")
{
psCommand.Parameters.Add("Server", machinename); // Add the target parameter
}
else if(command == "Get-ADReplicationPartnerMetadata")
{
psCommand.Parameters.Add("Server", machinename); // Add the target parameter
}

// Execute the command
var results = pipeline.Invoke();



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

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

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

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

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

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