PowerShellスクリプトを実行するアセンブリからカスタムコマンドレットを使用するC#コードからPowerShellスクリプトを実行しようとしています。コードは次のとおりです。Hosted PowerShellで同じアセンブリ内のコマンドレットが表示されない
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またはRunspaceなどでCmdletを登録するために必要なことはありますか?
魅力的な作品です!ありがとう! –