目前,我正在尝试创建我的第一个PowerShell Snapin。我遵循了本教程:How to create a PowerShell Snapin,一切正常,直到尝试调用自定义cmdlet。另外,我添加了一个“构建后事件”来注册程序集。
"C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" $(TargetPath)
[之后,我添加了Snapin,它就像一个咒语:
Add-PSSnapin CustomSrv.Commands
System.Automation参考的程序集路径:
C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll
作为目标平台,我选择了x86,并且还在x86 PowerShell中执行所有操作。平台设置为“活动(任何CPU)”
这是我的cmdlet代码:
namespace CustomSrv.Commands
{
[Cmdlet(VerbsCommunications.Connect, "CustomSrv")]
public class TestCmdlet1 : Cmdlet
{
protected override void BeginProcessing()
{
WriteObject("BeginProcessing() method - Execution has begun");
}
protected override void ProcessRecord()
{
WriteObject("ProcessRecord() method - Executing the main code");
}
protected override void EndProcessing()
{
WriteObject("EndProcessing() method - Finalizing the execution");
}
}
}
这是我尝试调用Cmdlet时遇到的错误:
PS C:\WINDOWS\system32> Connect-CustomSrv
Connect-CustomSrv: The term 'Connect-CustomSrv' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ Connect-CustomSrv
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Connect-CustomSrv:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我做错了,有关定位平台(x86)的设置有问题吗?
然后:PS C:> Set-ExecutionPolicy不受限制
希望它有效!