-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecureTokens.psm1
120 lines (102 loc) · 4.38 KB
/
SecureTokens.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
param(
[parameter(Position = 0, Mandatory = $false)]
[boolean] $Quiet = $false
)
#region Private Variables
# Current script path
[string] $script:ScriptPath = Split-Path (get-variable myinvocation -scope script).value.Mycommand.Definition -Parent
if ((Get-Variable MyInvocation -Scope script).Value.Line.Trim().Length -eq 0) { $Quiet = $true }
#endregion Private Variables
#region Private Helpers
# Dot sourcing private script files
Get-ChildItem $script:ScriptPath/private -Recurse -Filter "*.ps1" -File | ForEach-Object {
. $_.FullName
}
#endregion Load Private Helpers
#region public Helpers
[string[]] $script:showhelp = @()
# Dot sourcing public script files
Get-ChildItem $script:ScriptPath/public -Recurse -Filter "*.ps1" -File | ForEach-Object {
. $_.FullName
([System.Management.Automation.Language.Parser]::ParseInput((Get-Content -Path $_.FullName -Raw), [ref]$null, [ref]$null)).FindAll( { $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $false) | ForEach-Object {
Export-ModuleMember $_.Name
$script:showhelp += $_.Name
}
}
#endregion public Helpers
[string] $script:SecureTokenFolder = ''
[string] $script:DefaultCert = ''
try {
if (test-path "$script:scriptpath\config\FolderPath.txt") {
if (-not $Quiet) { Write-host "Attempting to load SecureTokens config file..." }
[string] $script:SecureTokenFolder = get-content "$script:scriptpath\config\FolderPath.txt"
if (-not $Quiet) { Write-Host " Loaded config file" -ForegroundColor Green }
if (-not $Quiet) {
if (test-path $script:SecureTokenFolder -ErrorAction SilentlyContinue) {
Write-Host " Path to SecureTokens ($($script:SecureTokenFolder)) is valid" -ForegroundColor Green
} else {
Write-Host " Path to SecureTokens ($($script:SecureTokenFolder)) is NOT valid" -ForegroundColor Yellow
Write-Host " Use the Set-SecureTokenFolder function "
}
}
} else {
if (-not $Quiet) { Write-host "Default config does not exist.... Fixing that problem.... " -ForegroundColor Yellow }
Set-SecureTokenFolder -default -clobber
if (-not $Quiet) {
Write-host " Use Set-SecureTokenFolder to override this default (if necessary)"
Write-host ""
}
}
} catch {
$script:SecureTokenFolder = ""
Write-Host "No default Token folder exists. Use 'Set-SecureTokenFolder' to create one"
}
try {
if (test-path "$script:scriptpath\config\DefaultCert.txt") {
if (-not $Quiet) { Write-host "Attempting to load Default Certificate config file..." }
[string] $script:DefaultCert = get-content "$script:scriptpath\config\DefaultCert.txt"
if (-not $Quiet) { Write-Host " Loaded config file" -ForegroundColor Green }
if (-not $Quiet) {
if ($script:DefaultCert) {
if (Find-STEncryptionCertificate -filter "${script:DefaultCert}$") {
Write-Host " SecureTokens Default Cert (" -NoNewline -ForegroundColor Green
Write-Host (Find-STEncryptionCertificate -filter "${script:DefaultCert}$").Subject -NoNewline -ForegroundColor Yellow
Write-Host ") is configured" -ForegroundColor Green
} else {
Write-Host " Saved Default Certificate is not valid for this user/machine"
}
} else {
Write-Host " SecureTokens Default Certs is NOT configured" -ForegroundColor Yellow
Write-Host " Use the Set-STDefaultCertificate function "
}
}
} else {
if (-not $Quiet) {
$script:DefaultCert = ''
Write-host "Default Certificate config does not exist.... " -ForegroundColor Yellow
Write-host " Use Set-STDefaultCertificate if you want to use Certificate Encryption by default"
Write-host ""
}
}
} catch {
$script:DefaultCert = ''
Write-host "Default Certificate config does not exist.... " -ForegroundColor Yellow
Write-host " Use Set-STDefaultCertificate if you want to use Certificate Encryption by default"
Write-host ""
}
if (test-path $script:ScriptPath\formats) {
try {
Update-FormatData $ScriptPath\formats\*.ps1xml -ErrorAction Stop
} catch { }
}
if (-not $Quiet) { Get-SecureTokenHelp }
###################################################
## END - Cleanup
#region Module Cleanup
$ExecutionContext.SessionState.Module.OnRemove = {
# cleanup when unloading module (if any)
Get-ChildItem alias: | Where-Object { $_.Source -match "SecureTokens" } | Remove-Item
Get-ChildItem function: | Where-Object { $_.Source -match "SecureTokens" } | Remove-Item
Get-ChildItem variable: | Where-Object { $_.Source -match "SecureTokens" } | Remove-Item
}
#endregion Module Cleanup