Windows Vista Forums

Pointers on how to structure scripts

  1. #1



    Junior Member
    Join Date : Dec 2006
    Posts : 24
    Local Time: 02:06 AM

    Pointers on how to structure scripts

    Hi,

    I have done a couple of scripts that follow a basic pattern.
    Now I want to modularize the code, so that it is not duplicated in the various scripts.
    See below for the basic outline of the scripts.

    I could lift the common code to a separate ps1 file,
    and call that with parameters. But that would mean that I have to create & dispose of the database objects every time I call that script.

    I also wonder, what happens when one calls another ps1 script from a script.
    Is there any overhead from doing that?



    As I understand, there is no method to "include/inherit" another ps1 file.
    But you could place common stuff in the profile.ps1 file, maybe that would be a good idea?

    Anyway, if anyone has any pointers or suggestions I would be happy to hear


    rockmoose


    PHP Code:
    $servers = @("S1","S2","S3","S4")

    # ----------------- Common to all ps scripts -------------------------------

    # code to initialize database connection, command and command parameters
    # create SqlConnection
    # create SqlCommand
    # create SqlParameters
    # add parameters to SqlCommand


    function saveToDb(parameterValues[])
    {
        
    # set parametervalues
        # open connection
        # executeSp
        # close connection
    }

    # ----------------- End Common to all scripts -------------------------------



    # ------------------ Particular to different counters -----------------------
    # gather performance counters and data from different machines
    foreach($server in $servers)
    {
        
    # getwmi data
        
    foreach($counterInstance)
        {
            
    saveToDb(instanceValues)
        }
    }
    # ------------------ End Particular to different counters -----------------------

    # --------------- Common --------------------------------------
    # dispose of SqlCommand
    # dispose of SqlConnection 

      My System SpecsSystem Spec

  2. #2


    Gaurhoth Guest

    Re: Pointers on how to structure scripts

    Actually you can 'inherit/include' another ps1 file by dot-sourcing another
    ps1 script - here is an example:

    Assume Script1.ps1 contains the following:

    function Hello-World {
    write-host "hello world"
    }

    --- end file ---

    You can now reference the Hello-World function from another script by "Dot
    Sourcing" script1.ps1 like so:

    .. \fullpath\Script1.ps1
    Hello-World

    --- end file ----

    That first line is a PERIOD followed by a space then the path to the script
    file to include. I don't claim to understand the choice in syntax of a DOT,
    but it works



    "rockmoose" <rockmoose.2jdexg@no-mx.vista64.net> wrote in message
    news:rockmoose.2jdexg@no-mx.vista64.net...
    >
    > Hi,
    >
    > I have done a couple of scripts that follow a basic pattern.
    > Now I want to modularize the code, so that it is not duplicated in the
    > various scripts.
    > See below for the basic outline of the scripts.
    >
    > I could lift the common code to a separate ps1 file,
    > and call that with parameters. But that would mean that I have to
    > create & dispose of the database objects every time I call that script.
    >
    > I also wonder, what happens when one calls another ps1 script from a
    > script.
    > Is there any overhead from doing that?
    >
    > As I understand, there is no method to "include/inherit" another ps1
    > file.
    > But you could place common stuff in the profile.ps1 file, maybe that
    > would be a good idea?
    >
    > Anyway, if anyone has any pointers or suggestions I would be happy to
    > hear
    >
    >
    > rockmoose
    >
    >
    >
    > PHP code:
    > --------------------
    > $servers = @("S1","S2","S3","S4")
    >
    > # ----------------- Common to all ps
    > scripts -------------------------------
    >
    > # code to initialize database connection, command and command parameters
    > # create SqlConnection
    > # create SqlCommand
    > # create SqlParameters
    > # add parameters to SqlCommand
    >
    >
    > function saveToDb(parameterValues[])
    > {
    > # set parametervalues
    > # open connection
    > # executeSp
    > # close connection
    > }
    >
    > # ----------------- End Common to all
    > scripts -------------------------------
    >
    >
    >
    > # ------------------ Particular to different
    > counters -----------------------
    > # gather performance counters and data from different machines
    > foreach($server in $servers)
    > {
    > # getwmi data
    > foreach($counterInstance)
    > {
    > saveToDb(instanceValues)
    > }
    > }
    > # ------------------ End Particular to different
    > counters -----------------------
    >
    > # --------------- Common --------------------------------------
    > # dispose of SqlCommand
    > # dispose of SqlConnection
    > --------------------
    >
    >
    > --
    > rockmoose
    > ------------------------------------------------------------------------
    > rockmoose's Profile: http://vista64.net/forums/member.php?userid=1022
    > View this thread: http://vista64.net/forums/showthread.php?t=30329
    >



      My System SpecsSystem Spec

  3. #3



    Junior Member
    Join Date : Dec 2006
    Posts : 24
    Local Time: 02:06 AM


      Thread Starter
    Thank You.

    This is getting better and better



    Is there a way to protect variables and/or functions to not be "overridden" or "reset"
    in files that DOT source another file?

    rockmoose

      My System SpecsSystem Spec

  4. #4


    Rob Kenny Guest

    Re: Pointers on how to structure scripts

    rockmoose wrote:
    > Thank You.
    >
    > This is getting better and better
    >
    >
    >
    > Is there a way to protect variables and/or functions to not be
    > "overridden" or "reset"
    > in files that DOT source another file?
    >
    > rockmoose
    >
    >


    If the value should not change at all you can use Set-Variable and mark it as a constant see below

    Set-Variable motd ‘http://blogs.msdn.com/PowerShell/rss.xml’ -option constant

    note that when using Set-Variable you do not need to prefix the variable with a $

    Rob Kenny

      My System SpecsSystem Spec

  5. #5



    Junior Member
    Join Date : Dec 2006
    Posts : 24
    Local Time: 02:06 AM


      Thread Starter
    I posted the scripts here : http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76834

    They read WMI counters and save them in a SQL database.
    If anyone is interested.

    rockmoose

      My System SpecsSystem Spec

Pointers on how to structure scripts

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mouse Pointers Learning VBA Vista General 8 11 Jan 2010
run .cmd scripts on remote machine (automating the MOSS install via scripts) Zoran PowerShell 1 21 Jul 2009
Seeking Pointers on UAC Neil Kiser Vista security 3 16 Feb 2008
Pointers Edisto Vista music pictures video 0 12 Sep 2007
Mouse pointers Anusidh Vista General 7 31 May 2006