简体   繁体   English

如何在C#中将Powershell命令一起管道传输

[英]How to Pipe Powershell Commands together in C#

I have a set of commands in Powershell 2 where the output of one function is the input to the next function, but in the output of the first function I can select a value from the result, which is to be the input to the next function. 我在Powershell 2中有一组命令,其中一个函数的输出是下一个函数的输入,但是在第一个函数的输出中,我可以从结果中选择一个值,该值将作为下一个函数的输入。

For example with 例如与

Get-RDUserSession | ? {$_.username -eq "myuser"} | Disconnect-User

My example, I am selecting a user called "myuser", and passing this user into a function called Disconnect-User to do something. 在我的示例中,我选择一个名为“ myuser”的用户,并将该用户传递给名为Disconnect-User的函数以执行某项操作。 All works in Powershell, but I would like to be able to replicate this in C# without having to write multiple lines of code. 所有这些都可以在Powershell中运行,但是我希望能够在C#中复制它而不必编写多行代码。

Is it possible to reproduce this in C# in one line like in Powershell? 是否可以像Powershell一样在C#中以一行形式重现?

Sure, you'd end up with something like (multiline to make it more readable on SO; though I also prefer to format my code like that 'in real life'): 当然,您最终会得到类似(多行代码,使其在SO上更具可读性;尽管我也更喜欢在“现实生活中”格式化代码):

GetRDUserSessionMethod().Where(u => u.username == "myuser")
                        .ToList()
                        .ForEach(DisconnectUser);

GetRDUserSessionMethod() would simply have to return an IEnumerable<SomeUserClass> . GetRDUserSessionMethod()只需返回IEnumerable<SomeUserClass>

ToList() is required because MS never decided to have such an extension method on IEnumerable<T> , which for various reasons (type safety!) would be a nice addition to the language. 由于MS从未决定对IEnumerable<T>拥有这种扩展方法,因此需要ToList() ,出于各种原因(类型安全!),该扩展方法将是对该语言的不错补充。 It's pretty trivial to write one yourself if you want to avoid doing a ToList() each time, quick and dirty example: 如果您想避免每次都执行ToList() ,那么编写一个自己的代码是很简单的,这是一个又快又脏的示例:

public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
    foreach (T element in source)
    { 
        action(element);
    }
}

There can be some discussion on whether this is more readable than a regular foreach { } statement - probably as many opinions as devs out there. 关于这是否比常规的foreach { }语句更具可读性,可能会有一些讨论-可能与开发人员一样多。 Personally, I don't have an issue with writing code like that. 就我个人而言,编写这样的代码没有问题。

command1 && command2 command1 && command2

Say to restart a service named "XYZ" using c# 说使用c#重新启动名为“ XYZ”的服务

net stop "XYZ" && net start "XYZ" 净停止“ XYZ”和&净开始“ XYZ”

Code Example is, 代码示例是,

using (Process command = new Process())
{        
    command.StartInfo = new ProcessStartInfo
    {
        CreateNoWindow = true,
        FileName = CommonStrings.RestartProccesCommand,
        Arguments = COMMAND1 && COMMAND2,
        LoadUserProfile = false,
        UseShellExecute = false,
        WindowStyle = ProcessWindowStyle.Hidden,
    };
    command.Start();
    command.WaitForExit(3000);
    exitCode = command.ExitCode;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM