none
Powershell cmdlet RRS feed

  • Question

  • Hi,

    i'm a quite beginner!

    how can i write a cmdlet?

    how can i have access to source of cmdlet?

    sorry if my questions are a bit stupid.

    Regards

    Monday, February 3, 2020 11:48 AM

All replies

  • You may start with learning the very basics of Powershell firstMicrosoft Channel 9 - Getting Started with Microsoft PowerShell 3.0



    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, February 3, 2020 1:00 PM
  • Hi Thierry

    Basically a cmdlet is a function with parameter (s).

    You would like to read the code (great thing to do, to learn how to same the same)

    If the cmdlet is in a PS Module, open the .psm1 file in the module folder. At this step, 2 ways

    • Functions are declared in this file
    • Functions are located in separates .ps1 file (generally in a Module subfolder call Public and Private. Public for public function you can call, Private for private function used only the Public functions)

    A second way : to build you own function.  

    • Launch ISE, Modify Menu, Start Snippets ... and you have an entry Cmdlet (Advanced Function) ...

    This could help to build your own functions (reuse code is the real power :-) ).

    Careful : some functions used .dll or compiled code, then you can't show it.

    to build your own function : Go Step by step

    • Simple function
    • Function with param
    • Advanced function

    Here some samples of functions and use

    # A simple function
    function Myfunction
    {  
        Write-Host "Call me with My Name : " -ForegroundColor Green -NoNewline
        Write-host "Conan the barbarian" -ForegroundColor Red
    }
    Myfunction
    
    # A function with one parameter
    function Myfunction ($Name)
    { 
        Write-Host "Call me with My Name : " -ForegroundColor Green -NoNewline
        Write-Host $Name -ForegroundColor Red
    }
    Myfunction -Name "Conan the barbarian"
    
    # an Advanced function
    function Myfunction
    {
     <#
    .Synopsis
       Return an order
    .DESCRIPTION
       Return an order
    .PARAMETER Name
      The Name of the bad guy
    .PARAMETER Victim
      The Name of the victim
    .PARAMETER Extra
      Switch to add more threat to the victim
    .EXAMPLE
       Myfunction -Name "Conan the barbarian" -Victim "Thierry"
       utters threats with parameters
    .EXAMPLE
        "Conan the barbarian", "Thierry" | Myfunction
        utters threats with parameters from the pipeline
    .EXAMPLE
       Myfunction "ffz" "fsf"
        utters threats with parameters defined by their position only
    .EXAMPLE
      Myfunction abc def -Extra
      utters threats with parameters defined by their position only and extra threat with switch parameter
    #>   
        [CmdletBinding()] # add common parameters to the function
        Param
        (
            # Name of the bad guy  
            [Parameter(Mandatory=$true,         # This parameter is mandatory
                       ValueFromPipeline=$True, # accept value from pipeline
                       Position=0)]             # define the position of the parameter
            $Name, 
    
             # Name of the poor guy
            [Parameter(Mandatory=$true,
                       ValueFromPipeline=$True, 
                       Position=1)]
            $Victim, 
    
            # Extra threat
            [Parameter(ValueFromPipeline=$True, 
                       Position=2)]
            [Switch]$Extra
        )
        
        Write-Host "Call me with My Name : " -ForegroundColor Green -NoNewline
        Write-Host $Name -ForegroundColor Red
        Write-Host "Or, I should break your nose "  -ForegroundColor Green -NoNewline
        Write-host $Victim -ForegroundColor Red -NoNewline
        Write-Host ", your skull and what's behind it ! Be careful !" -ForegroundColor Yellow
        If ($Extra)
            {
            Write-Host "you're still here, go learn powershell ! " -ForegroundColor Yellow
            }
    }
    Myfunction -Name "Conan the barbarian" -Victim "Thierry"
    # Access to the help of my advanced function 
    Help Myfunction -full
    


    You can also build your own script with internal function inside

    # declare your function

    function MyFunction {...}

    # Main script

    Do Stuff Myfunction -param1 -Param2

    ...Other stuff

    ...

    Like this, you have reuse code.

    A Last word : One function for One job. Don't build complex functions (i.e. query something and export to file). It's better to call the function and pipeline the result to .. what you want (Out-file, Export-Csv, ... other treatment, ...)

    Regards

    Olivier

    Tuesday, February 4, 2020 5:27 PM
  • Hi Thierry

    Basically a cmdlet is a function with parameter (s)

    ...................

    Like this, you have reuse code.

    A Last word : One function for One job. Don't build complex functions (i.e. query something and export to file). It's better to call the function and pipeline the result to .. what you want (Out-file, Export-Csv, ... other treatment, ...)

    Regards

    Olivier

    Wow ... do you really think that helps?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, February 4, 2020 7:22 PM
  • A function is not a CmdLet. In PowerShell a CmdLet can be used by creating an "Advanced Function" which assigns the pieces needed for it to be used as a CmdLet,

    Functions in PowerShell can be listed like this:

    Get-ChildItem function:

    CmdLets can be listed like this:

    Get-Command -CommandType Cmdlet

    Note that these lines return different things.

    To learn about PowerShell you must take the time to get training in formal PowerShell.  As you can see a user has made some bad assumptions about the definitions of PS terms.  This is usually due to lack of any formal training in PowerShell or in programming systems.  Guesses gleaned by looking at posts on the Internet will only get you more confused.  Most non-trained people who have some basic desktop support experience try to be helpful but their lack of technical training causes them to disseminate bad and misleading information.

    There are a number of good books that can teach programming and programming with PowerShell.  These books will teach you the correct terminology and teach correct ways to write scripts as well as how to design and write scripted CmdLets (Advanced Functions).

    Once you learn the basics of the technology PS has a very good help system that we can use to reference details when needed.

    Examples:

    help functions
    help Get-ChildItem
    help about_providers
    help Get-Command

    Once you have taken a tutorial you will understand how to use help and a huge list of commands that allow us to discover things about PowerShell and how it is used.  You cannot do this by guesswork or by asking others to help you.  Without basic training you will not recognize when an answer is wrong or misleading.

    As with all advanced technologies you must study to understand and use the technology.

    Learning to script properly with PowerShell


    \_(ツ)_/

    Wednesday, February 5, 2020 7:25 AM
  • waouuuh!

    thanks a lot for your advices!!

    I have a lot to learn

    Many thanks to all

    i have edited MSModules.psm1 and it's very hard to understand.

     Can you give me an EASY book to read please?

    Thursday, February 6, 2020 11:45 AM
  • Agree, that cmdlet is not a function. 

     

    @Thierry Boibary-Granier Based on you question, I'd recommend to start learning powershell with simple scripts (like getting local users, check installed updates, etc). After being familiar with basic ps functionality you can continue with functions, modules and cmdlets.

    Here is article for novice related functions and modules - https://www.hyper-v.io/creating-function-windows-powershell-saving-module/

    Thursday, February 6, 2020 2:06 PM
  • Don't start by writing scripts.  Start by learning PowerShell so you don't develop bad habits that will be very hard to overcome.

    The following free book is a good place to start.

    Windows      PowerShell™ 4: TFM

    It is free as a PDF and print copies can be purchased at Amazon.  It is a well accepted book written by two very experienced Windows admins.  It is a book written for Windows techs who have no experience with scripting or programming so it teaches both in an easy to understand way.


    \_(ツ)_/

    Thursday, February 6, 2020 4:23 PM
  • Agree, that cmdlet is not a function. 

     

    @Thierry Boibary-Granier Based on you question, I'd recommend to start learning powershell with simple scripts (like getting local users, check installed updates, etc). After being familiar with basic ps functionality you can continue with functions, modules and cmdlets.

    Here is article for novice related functions and modules - https://www.hyper-v.io/creating-function-windows-powershell-saving-module/

    Well it is not that easy.  Script CmdLets are advanced functions.  All CmdLets are not advanced functions as they can be written in any CLR language.  When I post "function" here I am referring to PowerShell functions as functions is a very broad and loose term which is more exactly defined for each language.  PowerShell has a narrow definition of "function" and defines "advanced function" as the method to build a script CmdLet.

    Sorting all of this out is necessary for building scripts, modules and understanding PowerShell.  It requires learning PowerShell formally.

    PS can be used with little knowledge of anything just by using it as a command shell.  It is designed to be simple for the tech with little more than GUI experience but to use it in any advanced way requires more formal instruction.


    \_(ツ)_/

    Thursday, February 6, 2020 5:05 PM