PowerShell Snapin Cmdlet CommandNotFoundException

问题描述 投票:1回答:2

目前,我正在尝试创建我的第一个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)的设置有问题吗?

powershell cmdlets cmdlet snap-in pssnapin
2个回答
1
投票
或将目标更改为AnyCPU,并在installutil中注册该管理单元两次。一次使用32位版本,也使用64位版本(C:\ Windows \ Microsoft.NET \ Framework64 \ v2.0.50727 \ installutil.exe)。

0
投票
以管理员身份运行powershell并编写:PS C:> Get-ExecutionPolicy -list范围执行政策----- ---------------MachinePolicy未定义UserPolicy未定义流程未定义CurrentUser未定义LocalMachine RemoteSigned

然后:PS C:> Set-ExecutionPolicy不受限制

希望它有效!

© www.soinside.com 2019 - 2024. All rights reserved.