基本上代码来自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命令提示符下运行它会显示失败的消息 .
1 回答
Per Nacht评论我将答案粘贴在:
对于任何有兴趣了解解决方案的人来说,原因很简单,系统不支持x64 . 解决方案只是在c#项目的属性中,使平台目标成为“任何CPU”而不是x86或x64 .
此外,Get-PSSnapin不会显示错误消息,但如果从Visual Studio的命令提示符运行它,它将说它正常工作;但是在powershell命令提示符下运行它会显示失败的消息 .