Is it possible to include standalone functions into cmdlet?

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Max Kozlov Max Kozlov 1 year, 10 months ago.

  • Author
    Posts
  • #27474
    Profile photo of GS
    GS
    Participant

    Hello,

    I have written cmdlet with several functions inside. Problem is that it's very difficult to debug and deploy. Is it possible to have some sort of placeholder for cmdlet and just specify where separate PS1 files are which will included in cmdlet, so during runtime when cmdlet is compiled it will pull those resources in. And I still would be able to debug/troubleshoot individual pieces as functions and deploy them separate from main cmdlet placeholder. Kind of what ASP include files are.

    G

  • #27489
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    If you have common functions that you use over and over again, you should consider making them a Module.. Once you've converted them to a module, you can install the module as required and reference it in your scripts and\or import it with your Powershell profile.

    The functions would not need to inside the script or function cluttering it up, however, keep in mind that your script(s) won't work without the reference module. Make sure the Module is stored centrally and documented in your script where to actually get the Module.

  • #27496
    Profile photo of GS
    GS
    Participant

    So I create one module placeholder and use import-module for all my small ones inside it or how it's going to work?
    For example I have module and defined 2 functions there CleanFiles and ResetIIS. I want to debug/develop CleanFiles and ResetIIS separately.
    How would I structure such deployment?

  • #27502
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    If your company was Tech Corp. You have functions you've created (which should be named using approved Powershell verbs). For instance, you have Reset-TcIIS and you create a module TCAdmin and place all of the functions that you use for administering of Tech Corp in this module (or a certain product\vendor\etc.). When you write scripts, you can then do:

    function Do-Something {
        Reset-TcIIS -Param
        Start-TcFileClean -Force
    
    }
    
    Import-Module TCAdmin
    Do-Something
    

    If the functions are specific to a particular product or support administration of a particular product, you can create separate modules.

  • #27504
    Profile photo of GS
    GS
    Participant

    I have a little the other way around requirement. I have TCAdmin module with 2 functions defined there like you mentioned called Reset-TCIIS and Start-TCFileClean.
    I'd like those 2 functions to be in separate files as functions (like PS1 files) so I can edit them and debug them like normal powershell scripts. Is it possible?

  • #27511
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You would have a TCAdmin.PSM1 file. You could open this file in ISE and update\debug the file as required. You can also use a central file share for the module:

    http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/08/powertip-import-a-powershell-module-from-a-shared-location.aspx

  • #27512
    Profile photo of GS
    GS
    Participant

    Yes, that's what I have now (central file share and module being load in default profile).
    Issue is that not editing but debugging module. I did now find effective way to debug modules the same way it's easily can be done with PS1 files. For example in IDE of PowerGUI I can specify which parameters I want to pass to function I'm currently debugging in PS1 file but there is no such way for a module. Can I dot source PS1 file in module or something which will allow me to utilize PS1 files with functions inside them as part of the module. Something to the tune of :
    1. I will have Reset-TCiis.ps1 file and function inside that file to be part of TCAdman.psm1 file. So whenever I do Import-Module TCAdmin it will pull Reset-TCiis.ps1 and export function inside it like an available cmdlet?

  • #27514
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    GS, you can have one psm1 file and several /ps1 files on your module
    in psm you import all soirce files like this
    # dot-source all function files
    Get-ChildItem -Path $PSScriptRoot\*.ps1 | Foreach-Object{ . $_.FullName }
    Export-ModuleMember –Function Reset-TcIIS, Start-TcFileClean

    and of course you can debug each of it separately dot sourcing it one by one

    or you cn debug it this way:
    you write

    Reset-TcIIS.ps1 as
    function Reset-TcIIS {
    [...]
    }
    # run function code with all needed parameters
    ResetTcIIS -force -computername host1

    after debugging you just remove or comment out last line and move file to module folder

You must be logged in to reply to this topic.