-
Notifications
You must be signed in to change notification settings - Fork 3
/
dbaSecurityScan.psm1
64 lines (57 loc) · 2.29 KB
/
dbaSecurityScan.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
$script:ModuleRoot = $PSScriptRoot
$PSModuleRoot = $PSScriptRoot
$script:PSModuleRoot = $PSScriptRoot
$script:dssModuleRoot = $PSScriptRoot
Write-verbose "$PSModuleRoot"
function Import-ModuleFile {
[CmdletBinding()]
Param (
[string]
$Path
)
if ($doDotSource) { . $Path }
else {
try {
$ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($Path))), $null, $null)
}
catch {
Write-Warning "Failed to import $Path"
}
}
}
#Yoinked from dbachecks until I get the tests to be Pester 4/5 agnostic
if ((Get-Module Pester).Version.Major -eq 5) {
Write-Verbose -Message "You have Pester version 5 in this session which is not compatible - Let me try to remove it"
try {
Remove-Module Pester -Force
$CompatibleInstalledPester = Get-Module Pester -ListAvailable | Where-Object { $Psitem.Version.Major -eq 4 } | Sort-Object Version -Descending | Select-Object -First 1
Write-Verbose -Message "Removed Version 5 trying to import version $($CompatibleInstalledPester.Version.ToString())"
Import-Module $CompatibleInstalledPester.Path -Scope Global
}
catch {
Write-Error -Message "Failed to remove Pester version 5 or import suitable version - Do you have Version 4* installed ?"
Break
}
}
else {
try {
$CompatibleInstalledPester = Get-Module Pester -ListAvailable | Where-Object { $Psitem.Version.Major -le 4 -and $Psitem.Version.Major -gt 3 } | Sort-Object Version -Descending | Select-Object -First 1
Write-Verbose -Message "Trying to import version $($CompatibleInstalledPester.Version.ToString())"
Import-Module $CompatibleInstalledPester.Path -Scope Global
}
catch {
Write-Error -Message "Failed to import suitable version - Do you have Version 4* installed ?"
Break
}
}
# Detect whether at some level dotsourcing was enforced
$script:doDotSource = $true
# Import all internal functions
foreach ($function in (Get-ChildItem ".\Internal\functions\*.ps1")) {
. Import-ModuleFile -Path $function.FullName
}
# Import all public functions
foreach ($function in (Get-ChildItem ".\Public\functions\*.ps1")) {
# . Import-ModuleFile -Path $function.FullName
. $function.fullname
}