简体   繁体   English

调用 PS 命令到字符串

[英]Invoked PS command to string

Is there way to convert invoked powershell command from C# to string?.有没有办法将调用的 powershell 命令从 C# 转换为字符串?

Let's say for example i have something like this:例如,假设我有这样的事情:

PowerShell ps = PowerShell.Create();
                    ps.AddCommand("Add-VpnConnection");
                    ps.AddParameter("Name", "VPN_" + ClientName);
                    ps.AddParameter("ServerAddress", VPN_SERVER_IP);
                    ps.AddParameter("AllUserConnection");
                    ps.AddParameter("SplitTunneling", true);
                    ps.AddParameter("TunnelType", "L2tp");

And i would like to save invoked command to log file.我想将调用的命令保存到日志文件中。

Can i somehow return whole command as string?我可以以某种方式将整个命令作为字符串返回吗?

I believe what you want essentially is this.我相信你想要的本质上就是这个。

PowerShell ps = PowerShell.Create();
ps.AddScript($"Add-VpnConnection -Name \"VPN_{ClientName}\" -ServerAddress {VPNServerIP} -AllUserConnection -SplitTunneling -TunnelType L2tp");
ps.Invoke();

The invoke return will contain a collection of PSObject so you can read it and save the information like you want in a log in c#.调用返回将包含 PSObject 的集合,因此您可以读取它并将所需的信息保存在 c# 的日志中。

Note: This answer does not solve the OP's problem.注意:此答案不能解决OP 的问题。 Instead, it shows how to capture a PowerShell command's output as a string in C#, formatted in the same way that the command's output would print to the display (console) , if it were run in an interactive PowerShell session. Instead, it shows how to capture a PowerShell command's output as a string in C#, formatted in the same way that the command's output would print to the display (console) , if it were run in an interactive PowerShell session.


Out-String is the cmdlet that produces formatted, for-display representations of output objects as strings , as they would print to the screen in a PowerShell console. Out-String是一个 cmdlet,它生成 output 对象作为字符串的格式化、显示表示,因为它们将在 PowerShell 控制台中打印到屏幕上。

Therefore, you simply need to use another .AddCommand() in order to pipe the output from your Add-VpnConnection call to Out-String :因此,您只需使用另一个.AddCommand()来从Add-VpnConnection调用到Out-String的 pipe output :

string formattedOutput;
using (PowerShell ps = PowerShell.Create())
{

  ps.AddCommand("Add-VpnConnection")
    .AddParameter("Name", "VPN_" + ClientName)
    .AddParameter("ServerAddress")
    .AddParameter("AllUserConnection", VPN_SERVER_IP)
    .AddParameter("SplitTunneling", true)
    .AddParameter("TunnelType", "L2tp");

  // Add an Out-String call to which the previous command's output is piped to.
  // Use a -Width argument (column count) large enough to show all data.
  ps.AddCommand("Out-String").AddParameter("Width", 512);

  // Due to use of Out-String, a *single string* is effectively returned,
  // as the only element of the output collection.
  formattedOutput = ps.Invoke<string>()[0];

}

Console.Write(formattedOutput);

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

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