header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Can I turn an AdvancedFunction in to a C# Cmdlet? (My1st CMDlet)
Last Post 11 Dec 2009 06:23 AM by PowerShell Jedi. 8 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
PowerShell JediUser is Offline
Basic Member
Basic Member
Posts:410
Avatar

--
10 Dec 2009 10:11 PM
    Hi all,
    I am playing around with Creating CMDlets for the first time. I have an Advanced Function which I would like to turn into a CMDlet. What I'm trying to accomplish is... I am trying to protect my Advanced Function code. If there's a better way to do that, please let me know.

    OK, so this is what I have so far...
    I downloaded the standard VisualStudio bits.
    Powershell templates &
    Windows Vista SDK

    This is the C# I have...
    -----------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Management.Automation;
    using System.Collections;

    namespace WindowsPowerShellTest
    {
    [Cmdlet("Invoke", "UndeadWraith", SupportsShouldProcess = true)]
    public class UndeadWraith : PSCmdlet
    {



    protected override void ProcessRecord()
    {
    try
    {

    }
    catch (Exception)
    {
    }
    }
    }
    }

    --------------------------------------------------
    I know the code goes in the "Try" section.
    My question is. Can I build a CMDlet like an Advanced Function? What I mean is can I add pipline objects and script to the "Try" section like you would with the "Process" section of an Advanced Function and if so what is the syntax.

    Thank You for your help.
    PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1642
    Avatar

    --
    11 Dec 2009 03:02 AM
    You're asking a question that is difficult to answer in a single blog post. Yes, you can definitely use your advanced function logic and create a C# cmdlet with it. The syntax will depend on what you're trying to do.

    Here's a simple cmdlet:
    http://msdn.microsoft.com/en-us/lib...85%29.aspx

    You'll find several examples in the PowerShell SDK online or locally if you install the latest Windows SDK.

    Don't forget to consider how you'll add your code to PowerShell either as a snapin or module.
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    PowerShell JediUser is Offline
    Basic Member
    Basic Member
    Posts:410
    Avatar

    --
    11 Dec 2009 03:40 AM
    Well My biggest problem, I think, is... I can't find any examples of how to use the contents of my Advanced Function, in a CMDlet. I am in no way a Dev., and so I don't know what the code should look like past the one line at a time that I find in each example. I was hoping for similar code to the way you call Powershell from c# out side on a CMDlet (http://www.devx.com/tips/Tip/42716), but I have no way of knowing how to even begin combining this code...

    ----------------------------

    Runspace runSpace = RunspaceFactory.CreateRunspace();
    runSpace.Open();

    Command getProcess = new Command("Get-Process");
    Command sort = new Command("Sort-Object");
    sort.Parameters.Add("Property", "VM");
    pipeline.Commands.Add(getProcess);
    pipeline.Commands.Add(sort);

    -----------------------------

    I think my frustration is the general lack of examples for the actual source code of a CMDlet. There endless Powershell script samples and examples, but the CMDlet examples are always only a few lines that usually referencing some Dot Net that has already been written by some one else

    WriteObject(System.Diagnostics.Process.GetProcesses(), true);

    etc...

    To quote one of the best commercials of all time.

    http://www.youtube.com/watch?v=Ug75diEyiA0>Where's the beef!?
    PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1642
    Avatar

    --
    11 Dec 2009 04:56 AM
    OK, you have to realize that you mention you're in no way a dev, but you're trying to write C#.

    I do think what you will find in the SDK is a good starting point, but a good starting point for someone that already is starting to have a grasp of C# programming...

    You may need to first decide on your goal(s). You want to write a cmdlet or create a PowerShell runspace to run your pipeline of commands or script.
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    PowerShell JediUser is Offline
    Basic Member
    Basic Member
    Posts:410
    Avatar

    --
    11 Dec 2009 05:25 AM
    I mentioned In my first post, What I'm trying to accomplish is... I am trying to protect my Advanced Function code. If there's a better way to do that, please let me know.

    I have a Function that I would like to protect because it has hard coded values. Values that I don't want others to be able to see like passwords. (that's just an example.)

    C# is no more difficult that powershell... IMHO.. I am patient, all it takes is learning the syntax. I have a basic grasp C#.


    I think what I'm basically trying to do is call powershell from C# from inside a CMDlet
    PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1642
    Avatar

    --
    11 Dec 2009 05:35 AM
    A side note:
    http://psobject.codeplex.com/wikipa...e=psobject

    Seems William has dropped that project.

    There's also a tool to create an .exe from PowerShell scripts in Sapien's PrimalScript, but that's $$$...

    I'll reply more in the next few days (if I remember).
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    PowerShell JediUser is Offline
    Basic Member
    Basic Member
    Posts:410
    Avatar

    --
    11 Dec 2009 05:35 AM
    [quote]
    Posted By marco.shaw on 11 Dec 2009 04:56 AM
    You may need to first decide on your goal(s). You want to write a cmdlet or create a PowerShell runspace to run your pipeline of commands or script.
    [/quote]

    Ok so.. Yah what I want to do is...


    Create a Powershell runspace to run my pipeline of commands and script that I can Invoke from the powershell command prompt :)
    PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)
    Steven MurawskiUser is Offline
    Community Co-Director
    Basic Member
    Basic Member
    Posts:137

    --
    11 Dec 2009 06:04 AM
    The structure of a cmdlet in C# is very similar to the advanced functions.

    Like you thought, the logic from your process block will go into the process method. The Try/Catch block is there for error handling.

    To accept pipeline input, you have to create your parameters. In a cmdlet written in C#, parameters are created as public properties that have the [Parameter()] attribute. Like in advanced functions, there are options for the [Parameter()] attribute. The PowerShell SDK on MSDN that Marco pointed you towards has more details on the various options for the parameter function, so I'm not going to duplicate that here.

    Any parameter that can accept values from the pipeline (either by property name or by type of object) can use pipeline input in your process method.

    You can also override the BeginProcessing and EndProcessing methods to do any setup or tear down that you may need.

    There is a cmdlet lifecycle diagram here -> http://msdn.microsoft.com/en-us/lib...S.85).aspx that will help show when different parameter types are bound (parameters that are not getting their values from the pipeline, versus those that are), and when each of the methods is called.

    If these concepts sound a little odd, you will likely need to brush up on your C# a bit. Writing a basic cmdlet is not that difficult, but writing a cmdlet in a language and process that is unfamiliar is a much more daunting task.

    We are more than happy to help you work through the process of creating a cmdlet to suit your needs, but lets focus this discussion on more specific parts of the process.

    On a side note, if you just want to compile your cmdlet to protect the script, you could set up your parameters and invoke your script in the cmdlet, passing whatever parameters you need to. http://msdn.microsoft.com/en-us/lib...S.85).aspx
    Steven Murawski
    Blog ( blog.usepowershell.com )
    Co-Host - Mind of Root ( www.mindofroot.com )
    PowerShell JediUser is Offline
    Basic Member
    Basic Member
    Posts:410
    Avatar

    --
    11 Dec 2009 06:23 AM
    Thank you Marco & Smurawski you have both been most helpful.
    PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer