3

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

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Dont know if repost, but:

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

Comments

1

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

3 Comments

As noted by the question's author, Import-Module loads Cmdlets when the module is stored locally but not when loaded from the network. After granting FullTrust to the network directory, Import-Module finds the contained Cmdlets. Thanks a lot!
Well, it did work. Then I tried setting my module up to work from a different share and that wasn't working, then I went back to the first share and can't get that working again, either. I've used CasPol.exe -ResolvePerm to confirm that my assembly is, in fact, being granted FullTrust. Very strange...
Figured it out. I was using the 32-bit version of caspol.exe yet launching 64-bit instances of PowerShell. Once I made the changes using the 64-bit version of caspol.exe it worked. Not sure why caspol.exe -resolveperm indicated my assembly was unrestricted when I'd granted FullTrust to its network path using the wrong version of caspol.exe, but oh well. See my answer below.
1

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"

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.