基本上代码来自 msdn.microsoft.com
构建代码后,我打开命令提示符并输入:Installutil -i %path%/Mycmdlets.dll
结果表明安装阶段成功完成,提交阶段也成功完成。但是,如果我去:
Get-PSSnapin -Registered ,仅显示 sqlServerCmdletSnapin,但我的 cmdlet 不存在。添加 pssnapin 也不起作用。
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.ComponentModel;
namespace Mycmdlets
{
[Cmdlet("hy", "Hello")]
public class GetHelloCommand : Cmdlet
{
protected override void EndProcessing()
{
WriteObject("Hello", true);
}
}
[RunInstaller(true)]
public class GetProcPSSnapIn01 : PSSnapIn
{
/// <summary>
/// Create an instance of the GetProcPSSnapIn01 class.
/// </summary>
public GetProcPSSnapIn01()
: base()
{
}
/// <summary>
/// Specify the name of the PowerShell snap-in.
/// </summary>
public override string Name
{
get
{
return "GetProcPSSnapIn01";
}
}
/// <summary>
/// Specify the vendor for the PowerShell snap-in.
/// </summary>
public override string Vendor
{
get
{
return "Microsoft";
}
}
/// <summary>
/// Specify the localization resource information for the vendor.
/// Use the format: resourceBaseName,VendorName.
/// </summary>
public override string VendorResource
{
get
{
return "GetProcPSSnapIn01,Microsoft";
}
}
/// <summary>
/// Specify a description of the PowerShell snap-in.
/// </summary>
public override string Description
{
get
{
return "This is a PowerShell snap-in that includes the get-proc cmdlet.";
}
}
/// <summary>
/// Specify the localization resource information for the description.
/// Use the format: resourceBaseName,Description.
/// </summary>
public override string DescriptionResource
{
get
{
return "GetProcPSSnapIn01,This is a PowerShell snap-in that includes the get-proc cmdlet.";
}
}
}
}
编辑:
对于任何有兴趣了解解决方案的人来说,原因只是系统不支持 x64。解决方案只是在 c# 项目的属性中,将平台目标设为“任何 CPU”而不是 x86 或 x64。
此外,Get-PSSnapin 不会显示错误消息,但如果您从 Visual Studio 的命令提示符运行它,它会说它工作正常;但是在 powershell 命令提示符下运行它会显示失败消息。