Search code examples
c#powershellpowershell-cmdlet

imported c# cmdlet not working


There are multiple project in my solution, one of which contains a class = basically a C# Module to be used via Power Shell console:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management;
using EDZ.DAL;
using EDZ.Model;
using System.Collections;

namespace AddUser
{
  [Cmdlet(VerbsCommon.Add, "User")]
  class AddUser : Cmdlet
  {
    protected override void ProcessRecord()
    {
      WriteObject("test string");
    }
    //public AddUser(IRepository<IEntity> irepo)
    //{
    //  repoUser = (RepositoryBase<User>)irepo;
    //}
    //RepositoryBase<User> repoUser;

    //RepositoryBase<User> repoUser = new RepositoryBase<User>();

    //[Parameter(Mandatory = false, Position = 1)]
    //public Guid UserID { get; set; }

    //[Parameter(Mandatory = false, Position = 2)]
    //public string FirstName { get; set; }

    //[Parameter(Mandatory = false, Position = 3)]
    //public string LastName { get; set; }

    //[Parameter(Mandatory = false, Position = 4)]
    //public string Title { get; set; }

    //[Parameter(Mandatory = false, Position = 5)]
    //public string Email { get; set; }

    //[Parameter(Mandatory = false, Position = 6)]
    //public Guid ExpertId { get; set; }

    //[Parameter(Mandatory = false, Position = 7)]
    //public Guid IdentityId { get; set; }
    //protected override void ProcessRecord()
    //{
    //  //try {
    //    User user = new User() { FirstName = this.FirstName, LastName = this.LastName, Title = this.Title, Email = this.Email, ExpertId = this.ExpertId, IdentityId = this.IdentityId };

    //    repoUser.Add(UserID, user);
    //  //}
    //  //catch(Exception ex)
    //  //{
    //  //  WriteObject("something's wrong: " + ex.Message);
    //  //}
    //  IEnumerable tempList = repoUser.GetAll();
    //  foreach(User u in tempList)
    //  {
    //    WriteObject(u);
    //  }
    //}
  }
}

What I did:

  1. built the solution and copied the path of the AddUser.dll

  2. opened the PS console as Administrator

  3. successfully imported the AddUser.dll -> when I check it via Get-Module command, AddUser is present in the Name column. Although, there is nothing in the ExportedCommands column in the same row...could that be the issue?

  4. When I write the Add-User command, I expect to display the "test string", but I get this error:

Add-User : The term 'Add-User' 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 + Add-User + ~~~~~~~~ + CategoryInfo : ObjectNotFound: (Add-User:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException


Solution

  • Class implementing cmdlet should be public or it will not be exported as command. As you does not use any explicit access modifier, your AddUser class is internal. You should make it public:

    public class AddUser : Cmdlet