简体   繁体   中英

Powershell Importing custom C# CMDlets, no available “ExportedCommands”

first question here :)

So I have to create a custom CMDLet for Powershell 2.0, using visual studio 2010 express. I have followed this seemingly simple tutorial : http://blogs.msdn.com/b/saveenr/archive/2010/03/08/how-to-create-a-powershell-2-0-module-and-cmdlet-with-visual-studio-2010-screencast-included.aspx

My code is almost the same (even tried copy pasting thier code) but after I call Import-Module "path_to_dll"

and then call Get-Module , i see my imported module, but no ExportedCommands are available.

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Binary     PowerShellCMDLetsLibrary  {}

C# Code:

namespace PowerShellCMDLetsLibrary
{
    [System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get,"RemedyXml")]
    public class Get_RemedyXml:System.Management.Automation.PSCmdlet
    {
    [System.Management.Automation.Parameter(Position = 0, Mandatory = true)]
    public string TicketID;

    protected override void ProcessRecord()
    {
    ...
    this.WriteObject(Result.InnerXml, true);
    }

Might be a blunder, I just can't see it

Two things jump out @ me:

  1. The TicketID is a field, not a Property.
  2. The overnamedspaced attribution makes the code hard to read.

I suspect it's #1 , but I can't see enough past #2 to be sure.

Hope this Helps

Dont know if repost, but:

I found the solution. Copied my dll from UNC networkpath to local c:\\ And now the command shows up.

If you are running this off a network or unc path , you must add the path to your .net trusts:

ie:

caspol -machine -addgroup 1. -url "file:\\\network\dir\\*" FullTrust  -n Uniquename

HTH,

Bob

This is due to Code Access Security in .NET. By default, an assembly loaded from a network share executes with reduced privileges, whereas one loaded from local storage has no restrictions at all. Unfortunately, the Import-Module cmdlet gives no indication that it failed to import the cmdlets within a module, even when called with the -Verbose parameter.

To change the set of permissions granted to a particular network location, use the caspol.exe utility to create a new code group for that location:

caspol.exe -machine -addgroup 1.2 -url "file://server/share/directory/*" FullTrust

The 1.2 in the above command refers to the LocalIntranet code group, which will be the new code group's parent. The following command will show which code groups are defined, and can be used to display the group that you created:

caspol.exe -machine -listgroups

Note that on 32-bit Windows caspol.exe is located in %WinDir%\\Microsoft.NET\\Framework\\CLR_VERSION\\ (where, for PowerShell 2.0, CLR_VERSION is v2.0.50727 ), and on 64-bit Windows another copy is located in %WinDir%\\Microsoft.NET\\Framework64\\CLR_VERSION\\ . The 32- and 64-bit versions each have their own security configuration file ( CONFIG\\security.config ), so you will need to make sure you apply each security change to both versions using their respective caspol.exe .

The following command can be used to display the permissions that will be granted to a particular assembly:

caspol.exe -resolveperm "//server/share/directory/assembly.dll"

I basically copied an pasted your code. I did a Get-Module ClassLibrary2. Also TicketID does work.

ModuleType Name                      ExportedCommands
---------- ----                      ----------------
Binary     ClassLibrary2             Get-RemedyXml



using System.Management.Automation;

namespace ClassLibrary1
{
    [Cmdlet(VerbsCommon.Get, "RemedyXml")]
    public class Class1 : PSCmdlet
    {
        [Parameter(Position = 0, Mandatory = true)] 
        public string TicketID;

        protected override void ProcessRecord()
        {
            WriteObject(TicketID);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM