jpsider.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
Write-Verbose 'Importing from [C:\projects\jpsider\jpsider\private]'
# .\jpsider\private\Disable-SSLValidation.ps1
function Disable-SSLValidation
{
    <#
    .SYNOPSIS
        Disables SSL certificate validation
    .DESCRIPTION
        Disable-SSLValidation disables SSL certificate validation by using reflection to implement the System.Net.ICertificatePolicy class.
        Author: Matthew Graeber (@mattifestation)
        License: BSD 3-Clause
    .NOTES
        Reflection is ideal in situations when a script executes in an environment in which you cannot call csc.ese to compile source code.
        If compiling code is an option, then implementing System.Net.ICertificatePolicy in C# and Add-Type is trivial.
    .EXAMPLE
        Disable-SSLValidation
    .LINK
        http://www.exploit-monday.com
    #>

    Set-StrictMode -Version 2
    # You have already run this function if ([System.Net.ServicePointManager]::CertificatePolicy.ToString() -eq 'IgnoreCerts') { Return }
    $Domain = [AppDomain]::CurrentDomain
    $DynAssembly = New-Object System.Reflection.AssemblyName('IgnoreCerts')
    $AssemblyBuilder = $Domain.DefineDynamicAssembly($DynAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run)
    $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('IgnoreCerts', $false)
    $TypeBuilder = $ModuleBuilder.DefineType('IgnoreCerts', 'AutoLayout, AnsiClass, Class, Public, BeforeFieldInit', [System.Object], [System.Net.ICertificatePolicy])
    $TypeBuilder.DefineDefaultConstructor('PrivateScope, Public, HideBySig, SpecialName, RTSpecialName') | Out-Null
    $MethodInfo = [System.Net.ICertificatePolicy].GetMethod('CheckValidationResult')
    $MethodBuilder = $TypeBuilder.DefineMethod($MethodInfo.Name, 'PrivateScope, Public, Virtual, HideBySig, VtableLayoutMask', $MethodInfo.CallingConvention, $MethodInfo.ReturnType, ([Type[]] ($MethodInfo.GetParameters() | ForEach-Object {$_.ParameterType})))
    $ILGen = $MethodBuilder.GetILGenerator()
    $ILGen.Emit([Reflection.Emit.Opcodes]::Ldc_I4_1)
    $ILGen.Emit([Reflection.Emit.Opcodes]::Ret)
    $TypeBuilder.CreateType() | Out-Null

    # Disable SSL certificate validation
    [System.Net.ServicePointManager]::CertificatePolicy = New-Object IgnoreCerts
    return $true
}

Write-Verbose 'Importing from [C:\projects\jpsider\jpsider\public]'
# .\jpsider\public\ConvertTo-AsciiArt.ps1
function ConvertTo-AsciiArt
{
    <#
    .SYNOPSIS
        Converts a Text string to ASCII art.
    .DESCRIPTION
        Converts a Text string to ASCII art.
    .PARAMETER Text
        Please provide a text string.
    .PARAMETER FontName
        Please provide a FontName. Default: big
    .PARAMETER URL
        Please provide a URL. Default: http://artii.herokuapp.com/make
    .NOTES
        Requires Internet, with default url.
    .EXAMPLE
        ConvertTo-AsciiArt -Text "Check Meowt"
    .LINK
        http://www.invoke-automation.blog
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Low"
    )]
    [OutputType([String])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory = $true)][String]$Text,
        [Parameter(Mandatory = $false)][String]$FontName = "big",
        [Parameter(Mandatory = $false)][String]$url = "http://artii.herokuapp.com/make"
    )
    if ($pscmdlet.ShouldProcess("Starting ConvertTo-AsciiArt function."))
    {
        try
        {
            # Convert the String
            $EncodedText = [uri]::EscapeDataString($Text)
            $url = "$url" + "?text=$EncodedText&font=$FontName"
            # Perform Rest Call
            Invoke-RestMethod -Uri $url
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName
            Throw "ConvertTo-AsciiArt: $ErrorMessage $FailedItem"
        }
    }
    else
    {
        # -WhatIf was used.
        return $false
    }
}
# .\jpsider\public\Open-ConsoleWindow.ps1
function Open-ConsoleWindow
{
    <#
    .SYNOPSIS
        Opens a specified number of Console Windows.
    .DESCRIPTION
        Opens a specified number of Console Windows.
    .PARAMETER Count
        Please provide a new Console Title.
    .PARAMETER Executable
        Please provide an Executable. (Powershell.exe is default)
    .EXAMPLE
        Open-ConsoleWindow -Count 2 -Executable PowerShell.exe
    .EXAMPLE
        Open-ConsoleWindow -Count 2 -Executable pwsh.exe
    .LINK
        http://www.invoke-automation.blog
    .NOTES
        none
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Low"
    )]
    [OutputType([String])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory = $true)][int]$Count,
        [ValidateSet("PowerShell.exe", "pwsh.exe")]
        [Parameter(Mandatory = $false)][string]$Executable = "PowerShell.exe"
    )
    if ($pscmdlet.ShouldProcess("Starting Open-ConsoleWindow function."))
    {
        try
        {
            $thisCount = 1
            do {
                Write-Output "Opening Conosle Window: $thisCount"
                if ($Executable -like 'PowerShell.exe'){
                    Start-Process -WindowStyle Normal "$Executable" "-NoExit Update-ConsoleTitle -Title Console:$thisCount"
                } else {
                    Start-Process -WindowStyle Normal "$Executable"
                }
                $thisCount++
            } while ($thisCount -le $Count)
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName
            Throw "Open-ConsoleWindow: $ErrorMessage $FailedItem"
        }
    }
    else
    {
        # -WhatIf was used.
        return $false
    }
}
# .\jpsider\public\Reset-Module.ps1
function Reset-Module
{
    <#
    .SYNOPSIS
        Updates a module, and Re-imports it.
    .DESCRIPTION
        Updates a module, and Re-imports it.
    .PARAMETER Name
        Please provide a valid module Name.
    .EXAMPLE
        Reset-Module -Name VMware.PowerCLI
    .LINK
        http://www.invoke-automation.blog
    .NOTES
        none
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Low"
    )]
    [OutputType([String])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory = $true)][string]$Name
    )
    if ($pscmdlet.ShouldProcess("Starting Reset-Module function."))
    {
        try
        {
            # Remove the Module from the Session/Console
            Write-Output "Removing Module $Name from Console/Session."
            Remove-Module -Name $Name -Force -Confirm:$false

            # Update the Module
            Write-Output "Updating Module $Name"
            Update-Module -Name $Name -Force -Confirm:$false

            # Import the Module to the Session/Console
            Write-Output "Importing Module $Name to Console/Session."
            Import-Module -Name $Name -Force
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName
            Throw "Reset-Module: $ErrorMessage $FailedItem"
        }
    }
    else
    {
        # -WhatIf was used.
        return $false
    }
}
# .\jpsider\public\Update-ConsoleTitle.ps1
function Update-ConsoleTitle
{
    <#
    .SYNOPSIS
        Changes the title in the Console.
    .DESCRIPTION
        Update-ConsoleTitle changes the title in the Console.
    .PARAMETER Title
        Please provide a new Console Title.
    .NOTES
        none
    .EXAMPLE
        Update-ConsoleTitle -Title "My New Title"
    .LINK
        http://www.invoke-automation.blog
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Low"
    )]
    [OutputType([String])]
    [OutputType([Boolean])]
    param(
        [Parameter(Mandatory = $true)][String]$Title
    )
    if ($pscmdlet.ShouldProcess("Starting Update-ConsoleTitle function."))
    {
        try
        {
            Write-Output "Updating Console title to: $Title"
            $Host.UI.RawUI.WindowTitle = "$Title"
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message
            $FailedItem = $_.Exception.ItemName
            Throw "Update-ConsoleTitle: $ErrorMessage $FailedItem"
        }
    }
    else
    {
        # -WhatIf was used.
        return $false
    }
}
Write-Verbose 'Importing from [C:\projects\jpsider\jpsider\classes]'