我正在尝试从我的 C# 代码运行 PowerShell 脚本,这将使用运行它们的程序集中的自定义 Cmdlet。这是代码:
using System;
using System.Management.Automation;
[Cmdlet(VerbsCommon.Get,"Hello")]
public class GetHelloCommand:Cmdlet
{
protected override void EndProcessing()
{
WriteObject("Hello",true);
}
}
class MainClass
{
public static void Main(string[] args)
{
PowerShell powerShell=PowerShell.Create();
powerShell.AddCommand("Get-Hello");
foreach(string str in powerShell.AddCommand("Out-String").Invoke<string>())
Console.WriteLine(str);
}
}
当我尝试运行它时,我得到了一个 CommandNotFoundException。我写错了我的 Cmdlet 吗?我需要做些什么来在 PowerShell 或运行空间中注册我的 Cmdlet 吗?