Question

Currently I'm trying to create my first PowerShell Snapin. I followed this tutorial: How to create a PowerShell Snapin and everything works fine until I'm trying to invoke my custom cmdlet. In addition I added a "Post Build Event" to register the assembly.

"C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" $(TargetPath)

Afterwards I added the Snapin and it worked like a charm:

Add-PSSnapin CustomSrv.Commands

Path to the assembly for the System.Automation reference:

C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0\System.Management.Automation.dll

As my Target Platform I selected x86 and I'm also executing everything in x86 PowerShell. Platform is set to "Active (Any CPU)"

This is my cmdlet code:

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");
        }
    }
}

This is the error I'm getting when I'm trying to invoke the 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

What I'm a doing wrong, is there something setup wrong concerning the targeting platform (x86)?

Was it helpful?

Solution

If you choose x86 as your platform you also need to make sure you are using the x86 version of PowerShell. See here on how to do that. You probably are using the x64 version of powershell, which is the default. See here some more information.

Or change the target to AnyCPU and register the snapin with installutil twice. Once with the 32 bits version and also with the 64 bits version (C:\Windows\Microsoft.NET\Framework64\v2.0.50727\installutil.exe).

OTHER TIPS


If you are making in some IDE like atom
You should open atom from PowerShell, like

just type atom there


Should work

If you are making in some IDE like atom
You should open atom from PowerShell, like

just type atom there


Should work

This fixed it for me: https://www.opentechguides.com/how-to/article/powershell/105/powershel-security-error.html

Run your PowerShell as admin and write:

 PS C:\> Get-ExecutionPolicy -list  

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

then:

PS C:\> Set-ExecutionPolicy unrestricted 

Hope it works!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top