目前我正在尝试创建我的第一个 PowerShell 管理单元。我遵循了本教程:如何创建 PowerShell 管理单元并且一切正常,直到我尝试调用我的自定义 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)的设置是否有问题?