imported c# cmdlet not working

自作多情 提交于 2019-12-12 04:39:29

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/35438270/imported-c-sharp-cmdlet-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!