none
How to structure a script?

    Pregunta

  • Hi,

    I am trying to write a VBScript that will query a lot of different vaues on a server as part of a process to determine if a particular machine meets set standards. The script will check things such as:

    Free disk space is at least 30%

    Page file must be 1.5 x RAM

    Certain specific patches are installed

    etc etc

    Each check should generate a TRUE or FALSE statement - there are around 15-20 checks I need to script.

    I'm a fairly novice scripter - my experience amounts to checking the script repository for a script similar to what I want to do, and changing it as necessary to get what I want. I'm comfortable enough with retrieving the data.

    In this case, I'm not sure how best the script would be structured. Should I use functions for each check or should it be one big long script? At the moment I would be writing a script for each check, and then running a batch or command script to execute each VBScript one after the other.

    Since my end goal is to generate an Excel spreadsheet with the results, I fear this is not ideal. Can anyone give me some advice?

    jueves, 19 de marzo de 2009 11:49

Respuestas

  • You can pass parameters to a Sub or a Function.  The difference between the two is that a Function can return a result, where a Sub can't. 

    So, if you wanted the script to run and produce a report at the end of the script on which areas have problems that need to be addressed, then I would use a Function.

    If you wanted the script to report problem areas as it runs, and no report at the end, then use a Sub.

    I think in this case it really depends on how you want to report the areas that need to be addressed.


    Here is a little pseudo code just to try and illustrate what I'm trying to communicate:


    Use for Functions:

    Main:
       Define constants, variables, and create objects
     
       resultsPage = boolPageFileProperties()
          If resultsPage == False Then
               Wscript.Echo "There was a problem with the page file config.  Check the log for more details"
         End If

       resultsHardDrive = boolHardDriveProperties()
          If resultsHardDrive == False Then
               Wscript.Echo "There was a problem with the hard drive config.  Check the log for more details"
          End If


        Function boolPageFileProperties()
           results = TRUE
           If PageFile is variable size Then
              Write to Log File "ERROR:  Page File is variable size"
              results = FALSE
           End If

           If PageFile size <> RAM + (RAM/2) Then
              Write to Log File "ERROR: PageFile size is incorrectly set"
              results = FALSE
           End If
        boolPageFileProperties = results
        End Function

       Function boolHardDriveProperties()
          results = TRUE
          If FreeSpace < 10% of hard drive size Then
             Write to Log File "ERROR: Hard drive low on free space"
             results = False
          End If
       boolHardDriveProperties = results
       End Function


    Use for Subs:


    Main:
       Define constants, variables, and create objects
     
       boolPageFileProperties

       boolHardDriveProperties
          

        Sub boolPageFileProperties
           If PageFile is variable size Then
              Wscript.Echo "ERROR:  Page File is variable size"
           End If

           If PageFile size <> RAM + (RAM/2) Then
              Wscript.Echo "ERROR: PageFile size is incorrectly set"
           End If
        End Sub

       Sub boolHardDriveProperties
          If FreeSpace < 10% of hard drive size Then
             Wcript.Echo "ERROR: Hard drive low on free space"
          End If
       End Sub



    I hope that helps.




    viernes, 20 de marzo de 2009 13:27

Todas las respuestas

  • I think I would use functions...I'm not sure I would use a function for each check though.  Rather use the functions to group similar checks.  Define the Excel object in the main portion of the script before you call your functions, this way you can write the results within each function.  If you wanted, you could create a function to write the data to the Excel spreadsheet and then call that function after each test to write the results.  That would probably make the code look cleaner.

    I'm a novice scripter as well, but I have taken several programming classes both undergrad and graduate level...so I'm just pulling on some of the code structuring techniques that I picked up in those classes.
    jueves, 19 de marzo de 2009 16:10
  • Thanks for the reply.

    I wasn't sure about functions as I wasn't passing anything to them - I just wanted them to carry out a set task.

    I've looked at using Sub, and it seems to work a treat. Take the pagefile example - I wanted the script to do the following:

    Create a WMI object

    Check that the page file is a single size (ie. not initital size and maximum size)
        If it is single, carry on
            If not, echo FALSE and exit
        Retrieve physical RAM size
        Make sure page file is 1.5 x RAM
            If not, echo FALSE and exit
            else echo TRUE and exit

    So I wrapped that code up in subPageFile and called it at the end - it worked fantastically well, meaning I can apply this to all of the other scripts I have done so far. :-)
    viernes, 20 de marzo de 2009 12:24
  • You can pass parameters to a Sub or a Function.  The difference between the two is that a Function can return a result, where a Sub can't. 

    So, if you wanted the script to run and produce a report at the end of the script on which areas have problems that need to be addressed, then I would use a Function.

    If you wanted the script to report problem areas as it runs, and no report at the end, then use a Sub.

    I think in this case it really depends on how you want to report the areas that need to be addressed.


    Here is a little pseudo code just to try and illustrate what I'm trying to communicate:


    Use for Functions:

    Main:
       Define constants, variables, and create objects
     
       resultsPage = boolPageFileProperties()
          If resultsPage == False Then
               Wscript.Echo "There was a problem with the page file config.  Check the log for more details"
         End If

       resultsHardDrive = boolHardDriveProperties()
          If resultsHardDrive == False Then
               Wscript.Echo "There was a problem with the hard drive config.  Check the log for more details"
          End If


        Function boolPageFileProperties()
           results = TRUE
           If PageFile is variable size Then
              Write to Log File "ERROR:  Page File is variable size"
              results = FALSE
           End If

           If PageFile size <> RAM + (RAM/2) Then
              Write to Log File "ERROR: PageFile size is incorrectly set"
              results = FALSE
           End If
        boolPageFileProperties = results
        End Function

       Function boolHardDriveProperties()
          results = TRUE
          If FreeSpace < 10% of hard drive size Then
             Write to Log File "ERROR: Hard drive low on free space"
             results = False
          End If
       boolHardDriveProperties = results
       End Function


    Use for Subs:


    Main:
       Define constants, variables, and create objects
     
       boolPageFileProperties

       boolHardDriveProperties
          

        Sub boolPageFileProperties
           If PageFile is variable size Then
              Wscript.Echo "ERROR:  Page File is variable size"
           End If

           If PageFile size <> RAM + (RAM/2) Then
              Wscript.Echo "ERROR: PageFile size is incorrectly set"
           End If
        End Sub

       Sub boolHardDriveProperties
          If FreeSpace < 10% of hard drive size Then
             Wcript.Echo "ERROR: Hard drive low on free space"
          End If
       End Sub



    I hope that helps.




    viernes, 20 de marzo de 2009 13:27
  • Can this be done in powershell? i need to know if remote servers pagefiles are set to 1.5 and 2x RAM
    jueves, 25 de agosto de 2011 21:48
  • Hi,

    You are posting a response to a thread that was closed more than two years ago. Please start a new question and give as much detail as possible.

    Thanks,

    Bill

    jueves, 25 de agosto de 2011 22:12
    Moderador
  • Heckler - the answer is yes.

     


    jv
    jueves, 25 de agosto de 2011 22:43
  • Hi JV, your a busyman on all these forum's...i don't know how you do it but i'm glad :)

    so would you like to show me how or is that too much to ask?

    jueves, 25 de agosto de 2011 22:51
  • Hi JV, your a busyman on all these forum's...i don't know how you do it but i'm glad :)

    so would you like to show me how or is that too much to ask?


    Might be WMI but you need to attend to Bill's issue.  Jumping on old threads makes thread get way too deep. This screw up teh load times.  Also opld threads that continuew are not really very helpful to pepole looking to find solutions.  I know.  I break these rules all too often.

    Start a new thread and I will try to help you.

    In fact.  I bet that Bill already has the answer for you.

     

     


    jv
    jueves, 25 de agosto de 2011 22:55