dotnet core 使用 PowerShell 脚本

栏目: 编程工具 · 发布时间: 6年前

内容简介:本文告诉大家如何在 dotnet core 通过 Host PowerShell 的方法使用 PowerShell 脚本本文提供的方法需要在 dotnet core 2.1 和以上的版本,对于 dotnet core 2.1 以下暂时只能通过命令行调用 PowerShell.exe 的方法调用在使用之前请创建一个 dotnet core 程序然后安装下面几个 Nuget 库

本文告诉大家如何在 dotnet core 通过 Host PowerShell 的方法使用 PowerShell 脚本

本文提供的方法需要在 dotnet core 2.1 和以上的版本,对于 dotnet core 2.1 以下暂时只能通过命令行调用 PowerShell.exe 的方法调用

在使用之前请创建一个 dotnet core 程序然后安装下面几个 Nuget 库

通过 System.Management.Automation.PowerShell 可以快速使用 PowerShell 脚本

使用 PowerShell.Create() 创建一个 PowerShell 类,在 PowerShell 类先添加脚本,然后就可以运行,运行函数会返回运行的返回的内容

public static void Execute(string command)
        {
            using (var ps = PowerShell.Create())
            {
                var results = ps.AddScript(command).Invoke();
                foreach (var result in results)
                {
                    Console.Write(result.ToString());
                }
            }
        }

如使用下面的脚本

([System.Management.Automation.ActionPreference], [System.Management.Automation.AliasAttribute]).FullName

可以直接调用上面封装的函数

Execute("([System.Management.Automation.ActionPreference], [System.Management.Automation.AliasAttribute]).FullName");

在命令行使用 dotnet run 可以看到输出

System.Management.Automation.ActionPreferenceSystem.Management.Automation.AliasAttribute

本文使用的代码请看 github

官方例子

https://stackoverflow.com/a/47777636/6116637


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ajax开发精要

Ajax开发精要

柯自聪 / 电子工业出版社 / 2006 / 45.00

书籍目录: 概念篇 第1章 Ajax介绍 2 1.1 Ajax的由来 2 1.2 Ajax的定义 3 1.3 Web应用程序的解决方案 5 1.4 Ajax的工作方式 7 1.5 小结 8 第2章 B/S请求响应机制与Web开发模式 9 2.1 HTTP请求响应模型 9 2.2 B/S架构的请求响应机......一起来看看 《Ajax开发精要》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具