【发布时间】:2011-09-27 06:43:06
【问题描述】:
我正在尝试从我的 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 吗?
【问题讨论】:
标签: c# powershell powershell-cmdlet