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