Repository navigation
Expand file tree
/
Copy pathforeScriptCore.psm1
More file actions
258 lines (194 loc) · 9.19 KB
/
Copy pathforeScriptCore.psm1
File metadata and controls
258 lines (194 loc) · 9.19 KB
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
# -----------------------------------------------------------------------------------------
# Script: foreScriptCore.psm1
# Author: Nigel Thomas
# Date: April 24, 2015
# Version: 1.0
# Purpose: This module contains the functions that are used to update the foreScript UI
#
# Project: foreScript
#
# -----------------------------------------------------------------------------------------
#
# (C) Nigel Thomas, 2015
#
#------------------------------------------------------------------------------------------
#region functions
function ConvertTo-WPRHTML {
Param (
$Template,
$TemplateHeading,
$Data
)
$TEMPL_JQUERY_VERSION = (Get-Content ".\Templates\scripts\jquery-1.9.1.min.js" -ReadCount 0 | Out-String )
$TEMPL_JQUERYDATATABLE_VERSION = (Get-Content ".\Templates\scripts\json.htmTable.js" -ReadCount 0 | Out-String )
$outputtemplate = (Get-Content ".\Templates\html\$Template" -ReadCount 0 | Out-String )
$tplparameter = @{ "TEMPL_JQUERY_VERSION" = "$TEMPL_JQUERY_VERSION";
"TEMPL_JQUERYDATATABLE_VERSION" = "$TEMPL_JQUERYDATATABLE_VERSION";
"TEMPL_HEADING" = "$TemplateHeading";
"TEMPL_DATA" = "$Data"
}
foreach ($key in $tplparameter.Keys) {
$outputtemplate = $outputtemplate.Replace($key, $tplparameter[$key])
}
$outputtemplate
}
function ConvertTo-FSHTML {
Param (
$Template,
$TemplateHeading,
$Data
)
$TEMPL_JQUERY_VERSION = (Get-Content ".\Templates\scripts\jquery-1.9.1.min.js" -ReadCount 0 | Out-String )
$TEMPL_JQUERYDATATABLE_VERSION = (Get-Content ".\Templates\scripts\json.htmTable.js" -ReadCount 0 | Out-String )
$outputtemplate = (Get-Content ".\Templates\html\$Template" -ReadCount 0 | Out-String )
$tplparameter = @{ "TEMPL_JQUERY_VERSION" = "$TEMPL_JQUERY_VERSION";
"TEMPL_JQUERYDATATABLE_VERSION" = "$TEMPL_JQUERYDATATABLE_VERSION";
"TEMPL_HEADING" = "$TemplateHeading";
"TEMPL_DATA" = "$Data"
}
foreach ($key in $tplparameter.Keys) {
$outputtemplate = $outputtemplate.Replace($key, $tplparameter[$key])
}
$outputtemplate
}
function Write-Console {
Param (
$Message
)
$xamlGUI.Control_ConsoleOutputRichTextBox.Dispatcher.Invoke( "Normal", [action]({
try {
$Text = $Message | Out-String -Width 10240
$msgcolor = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Colors]::GhostWhite)
$tr = New-Object System.Windows.Documents.TextRange($xamlGUI.Control_ConsoleOutputRichTextBox.Document.ContentEnd, $xamlGUI.Control_ConsoleOutputRichTextBox.Document.ContentEnd);
$tr.Text = $Text #+ "`r`n"
$tr.ApplyPropertyValue([System.Windows.Documents.TextElement]::ForegroundProperty, $msgcolor)
$xamlGUI.Control_ConsoleOutputRichTextBox.ScrollToEnd()
}
catch {
if ($_.Exception.InnerException) {
$ExceptionMessage = $_.Exception.InnerException
}
else {
$ExceptionMessage = $_.Exception.Message
}
$msgcolor = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Colors]::Red)
$tr = New-Object System.Windows.Documents.TextRange($xamlGUI.Control_ConsoleOutputRichTextBox.Document.ContentEnd, $xamlGUI.Control_ConsoleOutputRichTextBox.Document.ContentEnd);
$tr.Text = $ExceptionMessage #+ "`r`n"
$tr.ApplyPropertyValue([System.Windows.Documents.TextElement]::ForegroundProperty, $msgcolor)
$xamlGUI.Control_ConsoleOutputRichTextBox.ScrollToEnd()
}
}))
}
function Write-TabbedUI { Param ( $HTMLData, $Tab, $TabHeader ) try { if ([String]::IsNullOrEmpty($HTMLData )) { return } # Input - The enumeration value is 5. Operations are processed at the same priority as input. # Normal - The enumeration value is 9. Operations are processed at normal priority. This is the typical application priority. $xamlGUI.Control_DisplayResults.Dispatcher.Invoke( "Normal", [action]({ try { # Create Web Browser control $webbrowser = New-Object Windows.Controls.WebBrowser $webbrowser.NavigateToString($HTMLData) # Create Tabitem $tabitem = New-Object Windows.Controls.TabItem $tabitem.Header = $TabHeader $tabitem.Content = $webbrowser if ($Tab -eq 'Dhcp') { # Check if we have displayed this tabitem before by reading the tabitem header foreach ($item in $xamlGUI.Control_DisplayDHCPLeases.Items) { if ($item.Header -contains $tabitem.Header) { return } } $tabitem.ContextMenu = $xamlGUI.Window.FindResource('DHCPContextMenu') #$tabitem.Style = $xamlGUI.Window.FindResource('MetroTabItem') $xamlGUI.Control_DisplayDHCPLeases.Items.Add($tabitem) #$xamlGUI.Control_MainDisplay.SelectedIndex = 2 #$xamlGUI.Control_DisplayDHCPLeases.SelectedIndex = ($xamlGUI.Control_DisplayDHCPLeases.Items.Count - 1) } if ($Tab -eq 'Results') { $tabitem.ContextMenu = $xamlGUI.Window.FindResource('TabitemContextMenu') #$tabitem.Style = $xamlGUI.Window.FindResource('MetroTabItem') $xamlGUI.Control_DisplayResults.Items.Add($tabitem) #$xamlGUI.Control_MainDisplay.SelectedIndex = 3 #$xamlGUI.Control_DisplayResults.SelectedIndex = ($xamlGUI.Control_DisplayResults.Items.Count - 1) } } catch { if ($_.Exception.InnerException) { $ExceptionMessage = $_.Exception.InnerException } else { $ExceptionMessage = $_.Exception.Message } $xamlGUI.Control_ConsoleOutputRichTextBox.AppendText($ExceptionMessage) } })) } catch { if ($_.Exception.InnerException) { $ExceptionMessage = $_.Exception.InnerException } else { $ExceptionMessage = $_.Exception.Message } $xamlGUI.Control_ConsoleOutputRichTextBox.AppendText($ExceptionMessage) }}
function Write-MessageBox {
Param (
[String]$Message
)
[System.Windows.MessageBox]::Show($Message)
}
function Write-Exception {
Param (
$Message
)
$xamlGUI.Control_ConsoleOutputRichTextBox.Dispatcher.Invoke( "Normal", [action]({
try {
$Text = $Message | Out-String -Width 10240
$msgcolor = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Colors]::Coral)
$tr = New-Object System.Windows.Documents.TextRange($xamlGUI.Control_ConsoleOutputRichTextBox.Document.ContentEnd, $xamlGUI.Control_ConsoleOutputRichTextBox.Document.ContentEnd);
$tr.Text = $Text #+ "`r`n"
$tr.ApplyPropertyValue([System.Windows.Documents.TextElement]::ForegroundProperty, $msgcolor)
$xamlGUI.Control_ConsoleOutputRichTextBox.ScrollToEnd()
}
catch {
if ($_.Exception.InnerException) {
$ExceptionMessage = $_.Exception.InnerException
}
else {
$ExceptionMessage = $_.Exception.Message
}
$msgcolor = New-Object System.Windows.Media.SolidColorBrush([System.Windows.Media.Colors]::Red)
$tr = New-Object System.Windows.Documents.TextRange($xamlGUI.Control_ConsoleOutputRichTextBox.Document.ContentEnd, $xamlGUI.Control_ConsoleOutputRichTextBox.Document.ContentEnd);
$tr.Text = $ExceptionMessage #+ "`r`n"
$tr.ApplyPropertyValue([System.Windows.Documents.TextElement]::ForegroundProperty, $msgcolor)
$xamlGUI.Control_ConsoleOutputRichTextBox.ScrollToEnd()
}
}))
}
#endregion functions
# Set functions to read only
Set-Item -Path function:ConvertTo-WPRHTML -Options ReadOnly
Set-Item -Path function:ConvertTo-FSHTML -Options ReadOnly
Set-Item -Path function:Write-Console -Options ReadOnly
Set-Item -Path function:Write-TabbedUI -Options ReadOnly
Set-Item -Path function:Write-MessageBox -Options ReadOnly
Set-Item -Path function:Write-Exception -Options ReadOnly
# Export the functions that will be accessed outside the module
Export-ModuleMember -Function ConvertTo-WPRHTML, ConvertTo-FSHTML, Write-Console, Write-TabbedUI, Write-MessageBox, Write-Exception