简体   繁体   English

有没有一种方法可以捕获Powershell脚本执行的输出,而无需使用System.Management.Automation

[英]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密钥,但为什么不只使用适用于.NETAWS开发工具包呢?

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.

相关问题 System.Management.Automation中没有PowerShell? - No PowerShell in System.Management.Automation? 如何使用 C# Powershell System.Management.Automation 将 PSObject 转换为流输出或数据表 - How to to Convert PSObject to Stream output or DataTable using C# Powershell System.Management.Automation “写入主机”output 进入 Powershell System.Management.Automation 参考程序集 4.0 的位置 - Where "Write-Host" output goes in Powershell System.Management.Automation Reference assemblies 4.0 运行 powershell 脚本时云不加载文件或程序集“System.Management.Automation” - Cloud not load file or assembly 'System.Management.Automation' while running powershell script MVC System.Management.Automation Powershell执行中的访问被拒绝错误 - Access Denied error in MVC System.Management.Automation powershell execution 使用 System.Management.Automation 调用 powershell 时,如何从远程命令传递警告和详细流? - How to pass Warning and Verbose streams from a remote command when calling powershell using System.Management.Automation? 如何使用Powershell包装器System.Management.Automation从集合中添加/删除元素 - How to add/remove elements from collection using Powershell wrapper System.Management.Automation 从 System.Management.Automation 调用脚本时未设置 $PSScriptRoot - $PSScriptRoot not set when invoking script from System.Management.Automation 使用System.Management.Automation将数组传递给C#中的cmdlet - Pass array to cmdlet in c# using System.Management.Automation System.Management.Automation错误访问被拒绝 - System.Management.Automation error access denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM