header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
How do I organize my Scripts/Functions ?
Last Post 08 Nov 2009 02:46 AM by Rhino2310. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
vUncleUser is Offline
New Member
New Member
Posts:3
Avatar

--
21 Oct 2009 02:32 AM
    Hi Everybody.
    I'm Using Powershell for about 1 year now as a System Admin.
    I've written a lot of brief functions, its a kind of admin-toolbox.

    What I don't realy understand is the "dot-sourcing" mechanism.

    - Are the "dot-sourced" functions loaded into ram ?

    - If yes, is there any incidence on performances ?

    - Is only a link to the functions loaded ?

    Here are some options I have listed to organize my functions.

    Could you please comment and give the Pros and Cons in regard of best performances (especially powershell loading time).

    1°/ Write The functions into my profile.ps1


    2°/ Write the functions into one (or several) ps1 file(s) and "dot-source" from my profile.


    3°/ Create a Module and load it from my profile.


    And additional, for those 3 options which is the best way to proceed :
     
     - Put the functions into script ( profile, dot-sourced.ps1, module)
     
        Example : ( a short function that i'm using in many scriots to check if remote host is available before
        
    function f-IfPing {
            If ( $args[0] -ne $null) {
                $Global:Ping = (New-Object System.Net.NetworkInformation.Ping).Send($args[0],10)
                If ( $Ping.Status -eq [System.Net.NetworkInformation.IPStatus]::Success ) { $true }
                Else {$false} }
            Else { $false}
        }
     
     - Put the code into isolated scripts that will be called by functions in ( profile, dot-sourced.ps1, module )
        
        Example :
        
        
    function F-IfPing { Path\F-Ifping.ps1 $args[0] }
        
        Path\F-Ifping.ps1 :
            If ( $args[0] -ne $null) {
                $Global:Ping = (New-Object System.Net.NetworkInformation.Ping).Send($args[0],10)
                If ( $Ping.Status -eq [System.Net.NetworkInformation.IPStatus]::Success ) { $true }
                Else {$false} }
            Else { $false }
            
            
     I hope I wasn't too long, and that my english is good enough to make my questions clear ;-)
    vUncleUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    23 Oct 2009 12:28 AM
    Oooops,

    So no one has any idea about this ?

    Maybe some interesting links ?
    Joel "Jaykul" BennettUser is Offline
    Basic Member
    Basic Member
    Posts:112
    Avatar

    --
    23 Oct 2009 01:42 PM
    When you dot-source a file, it's loaded into RAM, and functions which have been preloaded this way will therefore run slightly faster than if you just create scripts in a folder that you add to your $Env:PATH ...

    There's virtually no difference in performance between putting a bunch of functions into scripts and dot sourcing those from your profile, versus putting them straight into the profile script. Either one will slow down loading of your PowerShell session, use some RAM, and speed up the eventual execution of the functions.

    One other part of this I can answer right away: DO NOT create functions that wrap scripts. Put the scripts in your PATH, or create ALIASES to point to them.

    I'm not sure there's an answer to the best way to handle this that is right for everyone, but I did want to point out that you'd overlooked the possibility of just sticking them in scripts and adding that folder to your PATH so that you don't have to preload them at all.

    P.S.: I wrote up some more thoughts about the comparison (and about how I do it) on my blog:
    Organizing Your PowerShell Scripts and Functions
    Dan BallUser is Offline
    Basic Member
    Basic Member
    Posts:154
    Avatar

    --
    23 Oct 2009 02:08 PM
    Thanks for the post! It answered questions I didn't even know I had! *grin*

    Actually, now that I'm moving beyond the beginner phase of PowerShell programming, I'm looking closer at keeping my code cleaner and organized. Your blog post helped me in understanding how to do things in a "better" way.
    vUncleUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    26 Oct 2009 08:36 AM
    Thanks for this answer and the related Blog Post.

    I understand that there is no "ONE" solution, so your explanations, makes things clear.


    Also this http://nightroman.spaces.live.com/blog/cns gives a good  solution.


    Rhino2310User is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    08 Nov 2009 02:46 AM
    Thanks for this answer and the related Blog Post.
    I understand that there is no "ONE" solution, so your explanations, makes things clear.


    Totally agree with above sentiments ...Jaykul rocks as a teacher
    Learning Heaps thankyou
    regards Rhino2310
    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