PSJumpStart.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#Get PSJumpStart function files
$FunctionLib = @(Get-ChildItem -Path $PSScriptRoot\Functions\*.ps1 -ErrorAction SilentlyContinue)
#Get Local module lib function files
$LocalModuleLib = @(Get-ChildItem -Path $PSScriptRoot\LocalLib\*.ps1 -ErrorAction SilentlyContinue)
#Get Local lib function files (script folder OR current folder)
$LocalLibPath=$MyInvocation.PSScriptRoot
if ([string]::IsNullOrEmpty($LocalLibPath)) {    
    $LocalLibPath=$PWD.Path    
} 
$LocalLib = @(Get-ChildItem -Path $LocalLibPath\LocalLib\*.ps1 -ErrorAction SilentlyContinue)

#$functionNames = @()

#Import PSJumpstart functions
foreach($Import in $FunctionLib) {
    try {
        . $Import.FullName
        #$functionNames += ($Import.Name).Replace(".ps1","")
    }
    catch {
        Write-Error -Message "Failed to import function $($Import.FullName): $_"
    }
}
#Import local lib functions (override any PSJumpstart modules)
foreach($Import in $LocalModuleLib) {
    try {
        . $Import.FullName
        #$functionNames += ($Import.Name).Replace(".ps1","")
    }
    catch {
        Write-Error -Message "Failed to import function $($Import.FullName): $_"
    }
}

#Import local lib functions (override any functions)
foreach($Import in $LocalLib) {
    try {        
        . $Import.FullName
        #$functionNames += ($Import.Name).Replace(".ps1","")
    }
    catch {
        Write-Error -Message "Failed to import function $($Import.FullName): $_"
    }
}

#Export-ModuleMember -Function $functionNames
Export-ModuleMember -Function *

#region useless code?
function IsVerbose {
[CmdletBinding()]
param() 
   [bool](Write-Verbose ([String]::Empty) 4>&1)
}

function verboseTest {
[CmdletBinding()]
param($message) 
    Write-Verbose $message   
    Write-Verbose $ExecutionContext.SessionState.Path 
}

function GatherErrorTest
{
    Begin
    {
        $Error.Clear()
        $ErrorActionPreference = "SilentlyContinue"
    }

    Process
    {
        Get-AdUser -Identity "CrashThisCall"
        Get-NetAdapter -Name "TheNetWayToHell"
    }
    End
    {
        #Check ALL errors (this was a bad idea!!)
        foreach($err in $Error) {
            Msg "Line " + $err.InvocationInfo.ScriptLineNumber + ":" + $err.Exception "ERROR"
        }

    }
}

#endregion

#region Unused Code (for reading purposes only)

#Get global defaults to use with $PSDefaultParameterValues
#Returns a Hashtable to load into $PSDefaultParameterValues
#The Defaults will be loaded accoring to priority:
# User settings from userID-file in caller location or current location(?) is prio 1
# LogonDomain (or machine name) XML-file in Module location is Prio 2
# Module name(s) settings is last in order.
#
#function GetGlobalDefaultsFromXmlFiles($CallerInvocation) {
# $result = New-Object System.Management.Automation.DefaultParameterDictionary
#
# foreach($settingsFile in (Get-SettingsFiles $CallerInvocation ".xml")) {
# #Write-Host $settingsFile
# if (Test-Path "$settingsFile") {
# [xml]$settings = Get-Content $settingsFile
# foreach($node in $settings.FirstChild.ChildNodes) {
# $cmdLetName = $node.Name
# foreach($setting in $settings.FirstChild.$cmdLetName.ChildNodes) {
#
# #We cannot have a wildcard in the XML-file so we use the point. (cruddy solution?)
# $key = ($cmdLetName).Replace('.','*') + ":" + $setting.Name
# if (!$result.ContainsKey($key)) {
#
# try {
# #Add value from XML (OR result from PS-code execution)
# $result.Add($key,(Invoke-Expression $setting.InnerText))
# } catch {
# $ex = $PSItem
# $ex.ErrorDetails = "Err adding $key from $settingsFile. " + $PSItem.Exception.Message
# throw $ex
# }
# }
# }
# }
# }
# }
#
# #Return Parameter Dictionary
# [System.Management.Automation.DefaultParameterDictionary]$result
#}

#These will only return the modules path no matter what!
#function Get-CallerLocation {
# Split-Path $script:MyInvocation.MyCommand.Path -Parent
# Split-Path -Leaf $MyInvocation.PSCommandPath
#}
#
#function InternalModuleTest {
# Get-CallerLocation
#}
#

#endregion