9

C#コードから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などに登録するために必要なことはありますか?

4

3 に答える 3

9

現在のコードスニペットでこれを行う最も簡単な方法は次のとおりです。

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();

        // import commands from the current executing assembly
        powershell.AddCommand("Import-Module")
            .AddParameter("Assembly",
                  System.Reflection.Assembly.GetExecutingAssembly())
        powershell.Invoke()
        powershell.Commands.Clear()

        powershell.AddCommand("Get-Hello"); 
        foreach(string str in powerShell.AddCommand("Out-String").Invoke<string>()) 
            Console.WriteLine(str); 
    } 
} 

これは、PowerShell v2.0を前提としています($ psversiontableを使用するか、著作権の日付(2009年)までにコンソールをチェックインできます)。win7を使用している場合は、v2を使用しています。

于 2011-09-26T21:37:44.850 に答える
5

さらに別の簡単な方法は、コマンドレットを実行スペース構成に登録し、この構成で実行スペースを作成して、その実行スペースを使用することです。

using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

[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();
        var configuration = RunspaceConfiguration.Create();
        configuration.Cmdlets.Append(new CmdletConfigurationEntry[] { new CmdletConfigurationEntry("Get-Hello", typeof(GetHelloCommand), "") });
        powerShell.Runspace = RunspaceFactory.CreateRunspace(configuration);
        powerShell.Runspace.Open();

        powerShell.AddCommand("Get-Hello");
        foreach (string str in powerShell.AddCommand("Out-String").Invoke<string>())
            Console.WriteLine(str);
    }
}

念のため、このアプローチでは、コマンドレットクラスをパブリックにする必要はありません。

于 2011-09-26T22:22:29.100 に答える
1

Powershellセッションで使用する前に、コマンドレットを登録する必要があります。通常、Powershellスナップインを使用してこれを行います。プロセスの概要は次のとおりです。

  • カスタムコマンドレットを作成します(この部分はすでに完了しています)
  • コマンドレットを含み、説明するPowershellスナップインを作成します
  • を使用してシステムに新しいスナップインをインストールしますInstallutil
  • を使用して特定のPowershellセッションにスナップインを追加しますAdd-PSSnapin

プロセスをかなり完全に説明しているMSDNに関するいくつかの有用な記事があります。

カスタムコマンドレットの作成について説明するByteBlocksの2部構成のシリーズもあります。あなたはすでにパート1と同等のことをしているように見えるので、そのシリーズはあなたの最善の策かもしれません。パート2をクイックリファレンスとして使用するだけで、すぐに使用できる場合があります。

于 2011-09-26T20:39:36.753 に答える