Skip to content

Instantly share code, notes, and snippets.

@altrive
Created November 23, 2013 14:41
Show Gist options
  • Select an option

  • Save altrive/7615293 to your computer and use it in GitHub Desktop.

Select an option

Save altrive/7615293 to your computer and use it in GitHub Desktop.
Custom PSmodule sample to load command defined separated files using dot-sourcing.
#Write-Host "Load script files..." -ForegroundColor DarkGray
$scriptFiles = Get-ChildItem "$PSScriptRoot\*.ps1" -Exclude "*.Tests.ps1" -Recurse
foreach ($script in $scriptFiles)
{
#Write-Host "`tLoad script:" $script.Name -ForegroundColor DarkGray
try
{
#TODO: Suppress $Error entry recorded after module loading completed(can't suppress inside psm1 module?)
. $script.FullName
}
catch [Management.Automation.ScriptRequiresException]
{
$errorRecord = $_
$exception = $errorRecord.Exception
switch ($errorRecord.FullyQualifiedErrorId)
{
"ScriptRequiresElevation"{
Write-Warning ("`tSkip Load script: {0,-30}(Require runas administrator)" -f $script.Name)
}
"ScriptRequiresMissingModules"{
Write-Warning ("`tSkip Load script: {0,-30}(Require missing modules: {1})" -f $script.Name, $exception.MissingPSSnapIns[0])
}
"ScriptRequiresMissingPSSnapIns"{
Write-Warning ("`tSkip Load script: {0,-30}(Require PSSnapin: {1})" -f $script.Name, $exception.MissingPSSnapIns[0])
}
"ScriptRequiresUnmatchedPSVersion"{
Write-Warning ("`tSkip Load script: {0,-30}(Require PowerShell version: {1})" -f $script.Name, $exception.RequiresPSVersion)
}
"RequiresShellIDInvalidForSingleShell"{
Write-Warning ("`tSkip Load script: {0,-30}(Require PowerShell host ShellID: {1})" -f $script.Name, $exception.RequiresShellId)
}
default{
throw
}
}
}
}
#TODO:Export only public members that match specific criteria.
#Export-ModuleMember -Function * -Alias * -Cmdlet * -Variable *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment