dotnet core 使用 PowerShell 脚本
Core 脚本 dotnet PowerShell 使用
2023-09-27 14:28:54 时间
本文告诉大家如何在 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
我搭建了自己的博客 https://blog.lindexi.com/ 欢迎大家访问,里面有很多新的博客。只有在我看到博客写成熟之后才会放在csdn或博客园,但是一旦发布了就不再更新
如果在博客看到有任何不懂的,欢迎交流,我搭建了 dotnet 职业技术学院 欢迎大家加入
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。
相关文章
- ASP.NET Core – Razor Syntax
- EF Core – Many to Many
- .net core 基于Dapper 的分库分表开源框架(core-data)
- [core]-ARMV7-A、ARMV8-A、ARMV9-A 架构简介
- IOS Core Animation Advanced Techniques的学习笔记(四)
- .NET Core(C#) System.Threading.Timer使用实现定时任务及示例代码
- .NET Core 3.0 (windows、linux、mac)安装配置
- .NET Core多平台开发体验[1]: Windows
- 如果没有Visual Studio 2015,我们如何创建.NET Core项目 ?
- Python 脚本运行时Segmentation fault (core dumped)
- 探索 dotnet core 为何在 Windows7 系统需要补丁的原因
- 2019-8-31-dotnet-core-输出调试信息到-DebugView-软件
- 2019-8-31-dotnet-core-使用-PowerShell-脚本
- dotnet core 编程规范
- 《ASP.NET Core项目开发实战入门》带你走进ASP.NET Core开发
- ASP.NET Core开发-Docker部署运行
- python小记--攻防世界hit-the-core解题脚本编写