PowerShell Snapin Cmdlet CommandNotFoundException

时间:2014-01-05 19:55:37

标签: powershell cmdlets cmdlet snap-in pssnapin

目前我正在尝试创建我的第一个PowerShell Snapin。我遵循了本教程:How to create a PowerShell Snapin,一切正常,直到我尝试调用自定义cmdlet。另外,我添加了一个“Post Build Event”来注册程序集。

"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)有什么设置错误吗?

2 个答案:

答案 0 :(得分:1)

如果您选择x86作为您的平台,您还需要确保使用x86版本的PowerShell。有关如何执行此操作,请参阅here。您可能正在使用PowerShell的x64版本,这是默认版本。请参阅here更多信息。

或者将目标更改为AnyCPU并使用installutil注册snapin两次。一次使用32位版本,也使用64位版本(C:\ Windows \ Microsoft.NET \ Framework64 \ v2.0.50727 \ installutil.exe)。

答案 1 :(得分:-2)

这对我来说是固定的:https://www.opentechguides.com/how-to/article/powershell/105/powershel-security-error.html

以管理员身份运行PowerShell并编写:

 PS C:\> Get-ExecutionPolicy -list  

 Scope     ExecutionPolicy
            -----     ---------------
    MachinePolicy     Undefined
       UserPolicy     Undefined
          Process     Undefined
      CurrentUser     Undefined
     LocalMachine     RemoteSigned

然后:

PS C:\> Set-ExecutionPolicy unrestricted 

希望它能起作用!