I'm finding myself writing a bunch of related functions dealing with different nouns (clusters, sql servers, servers in general, files, etc.) and put each of these groups of functions in separate files (say cluster_utils.ps1, for example). I want to be able to "import" some of these libraries in my profile and others in my powershell session if I need them. I have written 2 functions that seem to solve the problem, but since I've only been using powershell for a month I thought I'd ask to see if there were any existing "best practice" type scripts I could be using instead.
To use these functions, I dot-source them (in my profile or my session)... for example,
# to load c:\powershellscripts\cluster_utils.ps1 if it isn't already loaded
. require cluster_utils
Here are the functions:
$global:loaded_scripts=@{}
function require([string]$filename){
if (!$loaded_scripts[$filename]){
. c:\powershellscripts\$filename.ps1
$loaded_scripts[$filename]=get-date
}
}
function reload($filename){
. c:\powershellscripts\$filename.ps1
$loaded_scripts[$filename]=get-date
}
Any feedback would be helpful.