运行 来自 .NET Core 的 PowerShell
Running PowerShell from .NET Core
有没有办法从 .net-core 运行 PowerShell 脚本?
我正在尝试 运行 新的 .net 核心中的 PowerShell 脚本 'website\api'。
据我所知,为了在 .net 上使用 运行 PowerShell,我们需要添加
System.Management.Automation 命名空间。
这对于 .net core 是不可能的(或者我还没有找到合适的添加方式)。
有几个 NuGet 包也旨在将此 DLL 添加到项目中,但它们也不与 .net 核心兼容。
有没有办法在 .net 核心上执行此操作?
以下是我尝试过的一些链接,但其中 none 是特定于 .net 核心的:
http://www.powershellmagazine.com/2014/03/18/writing-a-powershell-module-in-c-part-1-the-basics/
Referencing system.management.automation.dll in Visual Studio
https://www.nuget.org/packages/System.Management.Automation/
The official answer 是 运行 当前不支持您自己的应用程序中的 PowerShell Core。可能最大的问题是缺少 .Net Core AppDomain.GetAssemblies()
,这可能会在 .Net Core 1.2 中修复。
看起来它在 .NET Core 2.0 和 PowerShell 6 Beta 3 中得到了很好的支持(尽管它在 Beta 1 和 2 中也得到了支持,但并不那么容易),这里是 link Host PowerShell documentation in the GitHub repo
他们提供了一个很好的示例应用程序来展示它 运行 .NET Core 2.0 和 PowerShell Core v6.0.0-beta.3 及更高版本:
https://github.com/PowerShell/PowerShell/tree/master/docs/host-powershell
为了将正确的包添加到我的 NuGet 包列表中,我确实需要添加 powershell-core 作为新的 NuGet 存储库位置:
https://powershell.myget.org/F/powershell-core/api/v3/index.json
然后我可以安装 NuGet 包:
install-package microsoft.powershell.sdk -version 6.0.0-rc
install-package microsoft.powershell.commands.diagnostics -version 6.0.0-rc
install-package microsoft.wsman.management -version 6.0.0-rc
所有这三个依赖项都是必需的,然后我可以在我的 asp.net 核心 MVC Web 应用程序中执行以下简单的 PowerShell 命令:
public class PowerShellHelper
{
public void Execute(string command)
{
using (var ps = PowerShell.Create())
{
var results = ps.AddScript(command).Invoke();
foreach (var result in results)
{
Debug.Write(result.ToString());
}
}
}
}
尽管 James Eby 的回答是正确的,但我发现了一些有用的新信息。
现在 PowerShell Core 可用于跨平台使用。而且它是开源的!
以下是 Microsoft 文章中的一些有用要点:
PowerShell 现在正式支持 macOS 和 Linux,包括:
- Windows 7、8.1 和 10
- Windows 服务器 2008 R2、2012 R2、2016
- Windows 服务器半年频道
- Ubuntu 14.04、16.04 和 17.04
- Debian 8.7+ 和 9
- CentOS 7
- 红帽企业 Linux 7
- OpenSUSE 42.2
- 软呢帽 25、26
- macOS 10.12+
日志记录
在 macOS 上,PowerShell 使用本机 os_log API 来登录到 Apple 的统一日志系统。在 Linux 上,PowerShell 使用 Syslog,一种无处不在的日志记录解决方案。
基于 SSH 的电源Shell远程处理
PowerShell 远程处理协议 (PSRP) 除了传统的基于 WinRM 的 PSRP 之外,现在还可以使用安全 Shell (SSH) 协议。 Source
重大更改link
感谢@Roman 和@JamesEby。
如果我们不能使用dotnet core 2.0或更高版本,我们可以使用Process
到运行 Windows中的PowerShell.exe。
路径是C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
,我们可以在这段代码中使用Process
。
var process = new Process
{
StartInfo = new ProcessStartInfo(@"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe",
script)
{
WorkingDirectory = Environment.CurrentDirectory,
RedirectStandardOutput = true,
CreateNoWindow = true,
}
};
process.Start();
var reader = process.StandardOutput;
return reader.ReadToEnd();
脚本值是 PowerShell 脚本和 reader.ReadToEnd()
return 电源 shell 输出文本。
参见:
有没有办法从 .net-core 运行 PowerShell 脚本?
我正在尝试 运行 新的 .net 核心中的 PowerShell 脚本 'website\api'。 据我所知,为了在 .net 上使用 运行 PowerShell,我们需要添加
System.Management.Automation 命名空间。
这对于 .net core 是不可能的(或者我还没有找到合适的添加方式)。 有几个 NuGet 包也旨在将此 DLL 添加到项目中,但它们也不与 .net 核心兼容。 有没有办法在 .net 核心上执行此操作? 以下是我尝试过的一些链接,但其中 none 是特定于 .net 核心的:
http://www.powershellmagazine.com/2014/03/18/writing-a-powershell-module-in-c-part-1-the-basics/
Referencing system.management.automation.dll in Visual Studio
https://www.nuget.org/packages/System.Management.Automation/
The official answer 是 运行 当前不支持您自己的应用程序中的 PowerShell Core。可能最大的问题是缺少 .Net Core AppDomain.GetAssemblies()
,这可能会在 .Net Core 1.2 中修复。
看起来它在 .NET Core 2.0 和 PowerShell 6 Beta 3 中得到了很好的支持(尽管它在 Beta 1 和 2 中也得到了支持,但并不那么容易),这里是 link Host PowerShell documentation in the GitHub repo
他们提供了一个很好的示例应用程序来展示它 运行 .NET Core 2.0 和 PowerShell Core v6.0.0-beta.3 及更高版本:
https://github.com/PowerShell/PowerShell/tree/master/docs/host-powershell
为了将正确的包添加到我的 NuGet 包列表中,我确实需要添加 powershell-core 作为新的 NuGet 存储库位置:
https://powershell.myget.org/F/powershell-core/api/v3/index.json
然后我可以安装 NuGet 包:
install-package microsoft.powershell.sdk -version 6.0.0-rc
install-package microsoft.powershell.commands.diagnostics -version 6.0.0-rc
install-package microsoft.wsman.management -version 6.0.0-rc
所有这三个依赖项都是必需的,然后我可以在我的 asp.net 核心 MVC Web 应用程序中执行以下简单的 PowerShell 命令:
public class PowerShellHelper
{
public void Execute(string command)
{
using (var ps = PowerShell.Create())
{
var results = ps.AddScript(command).Invoke();
foreach (var result in results)
{
Debug.Write(result.ToString());
}
}
}
}
尽管 James Eby 的回答是正确的,但我发现了一些有用的新信息。
现在 PowerShell Core 可用于跨平台使用。而且它是开源的!
以下是 Microsoft 文章中的一些有用要点:
PowerShell 现在正式支持 macOS 和 Linux,包括:
- Windows 7、8.1 和 10
- Windows 服务器 2008 R2、2012 R2、2016
- Windows 服务器半年频道
- Ubuntu 14.04、16.04 和 17.04
- Debian 8.7+ 和 9
- CentOS 7
- 红帽企业 Linux 7
- OpenSUSE 42.2
- 软呢帽 25、26
- macOS 10.12+
日志记录
在 macOS 上,PowerShell 使用本机 os_log API 来登录到 Apple 的统一日志系统。在 Linux 上,PowerShell 使用 Syslog,一种无处不在的日志记录解决方案。
基于 SSH 的电源Shell远程处理
PowerShell 远程处理协议 (PSRP) 除了传统的基于 WinRM 的 PSRP 之外,现在还可以使用安全 Shell (SSH) 协议。 Source
重大更改link
感谢@Roman 和@JamesEby。
如果我们不能使用dotnet core 2.0或更高版本,我们可以使用Process
到运行 Windows中的PowerShell.exe。
路径是C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
,我们可以在这段代码中使用Process
。
var process = new Process
{
StartInfo = new ProcessStartInfo(@"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe",
script)
{
WorkingDirectory = Environment.CurrentDirectory,
RedirectStandardOutput = true,
CreateNoWindow = true,
}
};
process.Start();
var reader = process.StandardOutput;
return reader.ReadToEnd();
脚本值是 PowerShell 脚本和 reader.ReadToEnd()
return 电源 shell 输出文本。
参见: