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


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

查看所有标签

猜你喜欢:

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

jQuery基础教程(第3版)

jQuery基础教程(第3版)

[美] Jonathan Chaffer、[美] Karl Swedberg / 李松峰 / 人民邮电出版社 / 2012-4 / 59.00元

内容简介: jQuery是功能强大却又简洁明快的轻量级JavaScript库,出自名家之手。在DOM操作、事件处理、Ajax特性以及动画特效等方面的出色表现,使它迅速从众多JavaScript库中脱颖而出,成为一颗闪亮的明星。 本书是《jQuery基础教程(第2版)》的升级版,涵盖了jQuery 1.6及1.7的各种新特性,内容源自著名jQuery资源网站LearningjQuery.......一起来看看 《jQuery基础教程(第3版)》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

MD5 加密
MD5 加密

MD5 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具