[英]Check if Powershell Module is installed in C#
Is it possible to check if a powershell module ist installed in C#?是否可以检查 C# 中是否安装了 powershell 模块?
I need a C# condition if a module is / or is not installed.如果模块已/或未安装,我需要一个 C# 条件。
I know how to use powershell in C#, but how become a response from Powershell?我知道如何在 C# 中使用 powershell,但如何成为 Powershell 的响应?
Thanks for reading my question and i hope anyone can give me some hints.感谢您阅读我的问题,我希望任何人都可以给我一些提示。
You can add a reference to System.Management.Automation
and use the PowerShell
to invoke Get-Module
cmdlet to check if a specific module has been installed:您可以添加对
System.Management.Automation
的引用,并使用PowerShell
调用Get-Module
cmdlet 以检查是否已安装特定模块:
var exists = false;
using (PowerShell ps = PowerShell.Create())
{
string moduleName = "something";
ps.AddScript($"Get-Module -ListAvailable -Name {moduleName}");
var result = ps.Invoke();
exists = result.Count > 0;
}
Note: To add a reference to System.Management.Automation
, you can install the NuGet package using Install-Package System.Management.Automation.dll
Or if you want to see where the dll is located on your system, using powershell console, you can see [PSObject].Assembly.Location
and pick it.注意:要添加对
System.Management.Automation
的引用,您可以使用Install-Package System.Management.Automation.dll
安装 NuGet 包,或者如果您想查看 dll 在系统上的位置,请使用 powershell 控制台,您可以看到[PSObject].Assembly.Location
并选择它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.