[英]Is there a way to capture powershell script executed output without using System.Management.Automation
The below method launches a powershell script and executes it 以下方法启动Powershell脚本并执行
private static void LaunchPowershell()
{
string exeDir = "H:\\aws-newAPIKey";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = @"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe";
startInfo.Arguments = exeDir + "\\newApiKey_wh1.ps1";
startInfo.WorkingDirectory = exeDir;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
This results in the following output in the command line: 这将在命令行中产生以下输出:
CreatedDate : 1/3/2018 7:20:16 PM
CustomerId :
Description : This is api key for customer
Enabled : True
Id : qraj84yl5h
LastUpdatedDate : 1/3/2018 7:20:16 PM
Name : newAPIKey7
StageKeys : {}
Value : 2LBtWluNX1XbgtDG0SPY1IQgnVDkZTwzmgY3kd60
What I want to do is to obtain the Value of the API key created in C#. 我要做的是获取在C#中创建的API密钥的值。 Is there a way to do this without using System.Management.Autmomation library?
有没有一种方法可以不使用System.Management.Autmomation库?
If you want to retrieve complex data from powershell by executing a process, then you could use ConvertTo-Json on the powershell object, and parse it in C# 如果要通过执行进程从powershell检索复杂数据,则可以在powershell对象上使用ConvertTo-Json ,然后在C#中对其进行解析
Although it looks like you're trying to create an API key for AWS, so why not just use the AWS SDK for .NET ? 尽管您似乎正在尝试为AWS创建API密钥,但为什么不只使用适用于.NET的AWS开发工具包呢?
Your PowerShell pipeline runs the newApiKey_wh1.ps1 script which presumably returns the output you have shown. 您的PowerShell管道运行newApiKey_wh1.ps1脚本,该脚本可能会返回您显示的输出。
Without knowing more about the script I'll have to guess, but you should be able to use the Select-Object
statement on the output that is returned by the script to just the value you want. 在不知道有关脚本的更多信息的情况下,我不得不猜测,但是您应该能够在脚本返回的输出上使用
Select-Object
语句,使其成为所需的值。
So, if the script does something like: 因此,如果脚本执行以下操作:
return $Key
Then you could try: 然后,您可以尝试:
return ($Key | Select-Object -ExpandProperty Value)
This will pull out just the value of the property named Value . 这将仅提取名为Value的属性的值 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.