3

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)?

Kevin Goedecke
  • 1,553
  • 2
  • 14
  • 26
  • 2
    You should be aware that snap-ins are deprecated (and have been since v2); you should be building modules instead. And yes, you [can create binary modules](http://csharpening.net/?p=738) – alroc Jan 05 '14 at 20:04
  • What is the powershell version you are using? Do a $PSVersionTable to make sure. And what is the .Net version you are using for your assembly? And like alroc says. Snapins are deprecated, look up modules. You can have pure PowerShell modules so you don't even need Visual Studio or care about targets and what not. – Lars Truijens Jan 05 '14 at 20:09
  • As I'm intending to use the Snapin with Pash, which only supports Snapins, sadly switching to Modules is not an option for me... I'm using .NET Version 3.5 and PowerShell Version 4. – Kevin Goedecke Jan 05 '14 at 20:17

4 Answers4

2

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).

Community
  • 1
  • 1
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
0

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

just type atom there


Should work
horizon
  • 67
  • 5
0

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

just type atom there


Should work
horizon
  • 67
  • 5
-2

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!

Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19
Sara
  • 1