HelperFunctions.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<#------------------------------------------------------------------------------------------------------------------
    Helper Functions
--------------------------------------------------------------------------------------------------------------------#>



<#
 helper function
 Get-StringHash Credits : Jeff Wouters
 ref: http://jeffwouters.nl/index.php/2013/12/Get-StringHash-for-files-or-strings/
#>


function Get-StringHash {
    [cmdletbinding()]
    param (
        [parameter(mandatory=$false,parametersetname="String")]$String,
        [parameter(mandatory=$false,parametersetname="File")]$File,
        [parameter(mandatory=$false,parametersetname="String")]
        [validateset("MD5","SHA1","SHA256","SHA384","SHA512","RIPEMD160")]
        [parameter(mandatory=$false,parametersetname="File")]
        [validateset("MD5","SHA1","SHA256","SHA384","SHA512","RIPEMD160")]
        [string]$HashType = "MD5"
    )
    switch ($PsCmdlet.ParameterSetName) { 
        "String" {
            $StringBuilder = New-Object System.Text.StringBuilder 
            [System.Security.Cryptography.HashAlgorithm]::Create($HashType).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))| ForEach-Object {
                [Void]$StringBuilder.Append($_.ToString("x2")) 
            }
            $Object = New-Object -TypeName PSObject
            $Object | Add-Member -MemberType NoteProperty -Name 'String' -value $String
            $Object | Add-Member -MemberType NoteProperty -Name 'HashType' -Value $HashType
            $Object | Add-Member -MemberType NoteProperty -Name 'Hash' -Value $StringBuilder.ToString()
            $Object
        } 
        "File" {
            $StringBuilder = New-Object System.Text.StringBuilder
            $InputStream = New-Object System.IO.FileStream($File,[System.IO.FileMode]::Open)
            switch ($HashType) {
                "MD5" { $Provider = New-Object System.Security.Cryptography.MD5CryptoServiceProvider }
                "SHA1" { $Provider = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider }
                "SHA256" { $Provider = New-Object System.Security.Cryptography.SHA256CryptoServiceProvider }
                "SHA384" { $Provider = New-Object System.Security.Cryptography.SHA384CryptoServiceProvider }
                "SHA512" { $Provider = New-Object System.Security.Cryptography.SHA512CryptoServiceProvider }
                "RIPEMD160" { $Provider = New-Object System.Security.Cryptography.CryptoServiceProvider }
            }
            $Provider.ComputeHash($InputStream) | Foreach-Object { [void]$StringBuilder.Append($_.ToString("X2")) }
            $InputStream.Close()
            $Object = New-Object -TypeName PSObject
            $Object | Add-Member -MemberType NoteProperty -Name 'File' -value $File
            $Object | Add-Member -MemberType NoteProperty -Name 'HashType' -Value $HashType
            $Object | Add-Member -MemberType NoteProperty -Name 'Hash' -Value $StringBuilder.ToString()
            $Object           
        }
    }
}


<#
    Helper function to get the script and the line number of the calling function
#>

function getCallerInfo 
{
[CmdletBinding()]
param(
    #number of levels to go back in the call stack
    [ValidateRange(1,  99)]
    [int]$level = 2,
    [Switch]$FullStack 

)
    $dict = New-Object 'system.collections.generic.dictionary[[string],[string]]'
    try { 
        #Get the caller info
        $Stack = Get-PSCallStack
        #The level to track back should not exceed the depth of the callstack, so limit it where needed
        $level = [Math]::Min( $level, $Stack.Length -1 )
        #$caller = $Stack[$level]
        #Get Base information straight from the Stack
        $dict.Add('Command',            $Stack[$level].Command )
        $dict.Add('ScriptLineNumber',   $Stack[$level].ScriptLineNumber)
        $dict.Add('Position',           $Stack[$level].Position)
        $dict.Add('FunctionName',       $Stack[$level].FunctionName)
        $dict.Add('Location',           $Stack[$level].Location)

        #Extract the scriptname from the location
        $Scriptname = $Stack[$level].Location
        if ( [string]::IsNullOrEmpty( $Scriptname ) -ne $true ) {
            #Split on : and take the first node only
            
            $dict.Add('Script',   $Scriptname.Split(':')[0])
        }
         # Also Add the complete Stack
        If ($FullStack) {
            #$ReportLevels = 1 + $Stack.Count - $level
            $StackTrace = $Stack | Select -Skip $level -Property ScriptName,ScriptLineNumber,FunctionName,Command,Location,Arguments | ConvertTo-Json -Compress
            $dict.Add( 'PSCallStack', $StackTrace)
        }
  
        return $dict

    } catch { return $null}
}

<#
    Helper function to get the calling script or module version
    checks Script, Invocation Info, Module and Folder names
#>

function getCallerVersion 
{
[CmdletBinding()]
param(
    #Get version from X levels up in the call stack
    [int]$level = 2 #Use 2 as default as this is mostly an internal function, so we need to reach one additional Stacklevel up
)

    #Get the caller info
    $Stack = Get-PSCallStack
    
    #The level to track back should not exceed the depth of the callstack, so limit it where needed
    $level = [Math]::Min( $level, $Stack.Length -1 )
    Write-Verbose "getCallerVersion -level $level"
    #Default Caller Version to 0.0
    [Version]$CallerVersion = '0.0'
    try { 
        #Get the caller info
        $caller = $Stack[$level] 
        
        #if script
        if ( -NOT [string]::IsNullOrEmpty( $caller.ScriptName)){
            Write-Verbose "Try Test-ScriptFileInfo -Path $($caller.ScriptName)"
            $info = Test-ScriptFileInfo -Path $caller.ScriptName -ErrorAction SilentlyContinue
            if ( $info ) {
                $CallerVersion = $info.Version
                Write-Verbose "getCallerVersion found script version $CallerVersion"
                return $CallerVersion
            }
        } else {
            Write-Debug 'No scriptname'
        }
              
    } catch { 
        Write-Verbose 'catch error during script test' 
    } 
    Try {
        if (-NOT [string]::IsNullOrEmpty(  $Caller.InvocationInfo.MyCommand)){
            Write-Verbose "Try to extract version info from Stack[x].InvocationInfo.MyCommand "
            #define Regex to look for version
            $rxGetVersion = [regex]'\.VERSION\s+(?<Version>[\d\.]+)'

            if ($Caller.InvocationInfo.MyCommand.ToString() -match $rxGetVersion ) {
                #version is a named capture block
                $CallerVersion =  $Matches['Version']

                Write-Verbose "getCallerVersion found InvocationInfo.MyCommand version $CallerVersion"
                return $CallerVersion
            }
        } else {
            #convert to json
            $InvocationJSON = $Caller.InvocationInfo | ConvertTo-Json 
            Write-Verbose "Try to extract version info from Stack[x].InvocationInfo"
            #define Regex to look for version
            $rxGetVersion = [regex]'\.VERSION\s+(?<Version>[\d\.]+)'

            if ($InvocationJSON -match $rxGetVersion ) {
                #version is a named capture block
                $CallerVersion =  $Matches['Version']

                Write-Verbose "getCallerVersion found InvocationInfo version $CallerVersion"
                return $CallerVersion
            }
        }

     } catch { 
        Write-Verbose 'catch error during InvocationInfo test' 
    } 
    Try {
        
        $Filename = [System.IO.Path]::ChangeExtension( $caller.ScriptName, 'psd1')
        Write-Verbose "Try Test-ModuleManifest -Path $Filename"
        $info = Test-ModuleManifest -Path $Filename -ErrorAction SilentlyContinue
        if ( $info ) {
            $CallerVersion = $info.Version
            Write-Verbose "getCallerVersion found Module version $CallerVersion"
            return $CallerVersion
        }
    } catch { 
        Write-Verbose 'catch error during module test' 
    } 

    try {
        Write-Verbose "try to find a version from the path and folder names"
        $Folders= @( $Filename.Split('\') )
        $found = $false
        foreach( $f in $Folders ) {
            Try { 
                Write-Verbose "evaluating Path fragment [$f]"
                $CallerVersion = [version]$f ; $found = $true
            } 
            catch {}
        }
        if ($found) {
            #return last found version
            Write-Verbose "getCallerVersion found Folder version $CallerVersion"
            return $CallerVersion
        }
    } catch {
        Write-Verbose "getCallerVersion no version found"         
        return $CallerVersion
    }
    Write-Verbose "no version found"
    return $CallerVersion
}

<#
 # Credits: Joel Bennet
 # http://poshcode.org/4968
 
 Changed to not allow Nulls by default
#>



function ConvertTo-Hashtable {
  #.Synopsis
  # Converts an object to a hashtable of property-name = value
  PARAM(
    # The object to convert to a hashtable
    [Parameter(ValueFromPipeline=$true, Mandatory=$true)]
    $InputObject,

    # Forces the values to be strings and converts them by running them through Out-String
    [switch]$AsString,  

    # If set, allows each hashtable to have it's own set of properties, otherwise,
    # each InputObject is normalized to the properties on the first object in the pipeline
    [switch]$jagged,

    # If set, empty properties are Included
    [switch]$AllowNulls
  )
  BEGIN { 
    $headers = @() 
  }
  PROCESS {
    if(!$headers -or $jagged) {
      $headers = $InputObject | get-member -type Properties | select -expand name
    }
    $output = @{}
    if($AsString) {
      foreach($col in $headers) {
        if($AllowNulls -or ($InputObject.$col -is [bool] -or ($InputObject.$col))) {
          $output.$col = $InputObject.$col | out-string -Width 9999 | % { $_.Trim() }
        }
      }
    } else {
      foreach($col in $headers) {
        if($AllowNulls -or ($InputObject.$col -is [bool] -or ($InputObject.$col))) {
          $output.$col = $InputObject.$col
        }
      }
    }
    $output
  }
}