-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsNGENposh.psm1
189 lines (162 loc) · 5.94 KB
/
psNGENposh.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
#=======================================================================#
#
# Author: Collin Chaffin
# Last Modified: 09-10-2019 12:00PM
# Filename: psNGENposh.psd1
#
#
# Changelog:
#
# v 1.0.0.1 : 09-10-2019 : Initial release
#
# Notes:
#
# This Powershell module performs various SYNCHRONOUS ngen functions
#
# Since the purpose of this module is to for interactive use,
# I intentionally did not include any "Queue" options.
#
#
#
# Installation Instructions:
#
# Install via the PSGallery via:
#
# Install-Module psNGENposh -AllowPrerelease -Scope AllUsers
#
# Once installed, open Windows Powershell and execute:
#
# Import-Module psNGENposh
#
# To invoke ngen on currently loaded assembles, skipping those already generated:
#
# PS C:\> Invoke-NGENposh
#
# To invoke ngen on currently loaded assembles (ensure up to date even if cached):
#
# PS C:\> Invoke-NGENposh -Force
#
# To invoke ngen to regenerate cache for all system assemblies (*SEE WARNING BELOW**):
#
# PS C:\> Invoke-NGENposh -All
#
# **WARNING: The '-All' switch since the execution is SYNCHRONOUS will
# take considerable time, and literally regenerate all the
# global assembly cache. There should theoretically be no
# downside to this, but bear in mind other than time (and cpu)
# that since all the generated cache files are new, any
# system backups will consider those files as new and may
# likely cause your next incremental backup to be much larger
#
#=======================================================================#
#Requires -Version 5.0
using namespace System.Management.Automation
# INTERNAL HELPER
function Write-InfoInColor
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[String]$Message,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[System.ConsoleColor[]]$Background = $Host.UI.RawUI.BackgroundColor,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[System.ConsoleColor[]]$Foreground = $Host.UI.RawUI.ForegroundColor,
[Switch]$NoNewline
)
[HostInformationMessage]$outMessage = @{
Message = $Message
ForegroundColor = $Foreground
BackgroundColor = $Background
NoNewline = $NoNewline
}
Write-Information $outMessage -InformationAction Continue
}
function Invoke-NGENposh
{
<#
.SYNOPSIS
This Powershell function performs various SYNCHRONOUS ngen functions
.DESCRIPTION
This Powershell function performs various SYNCHRONOUS ngen functions
Since the purpose of this module is to for interactive use,
I intentionally did not include any "Queue" options.
.PARAMETER All
Regenerate cache for all system assemblies
.PARAMETER Force
Invoke ngen on currently loaded assembles (ensure up to date even if cached)
.EXAMPLE
To invoke ngen on currently loaded assembles, skipping those already generated:
PS C:\> Invoke-NGENposh
.EXAMPLE
To invoke ngen on currently loaded assembles (ensure up to date even if cached):
PS C:\> Invoke-NGENposh -Force
.EXAMPLE
To invoke ngen to regenerate cache for all system assemblies (*SEE WARNING BELOW**):
PS C:\> Invoke-NGENposh -All
.NOTES
**WARNING: The '-All' switch since the execution is SYNCHRONOUS will
take considerable time, and literally regenerate all the
global assembly cache. There should theoretically be no
downside to this, but bear in mind other than time (and cpu)
that since all the generated cache files are new, any
system backups will consider those files as new and may
likely cause your next incremental backup to be much larger
#>
param
(
[switch]$All,
[switch]$Force
)
Write-InfoInColor "`n===================================================================================" -Foreground 'DarkCyan'
Write-InfoInColor " BEGINNING TO NGEN " -Foreground 'Cyan'
Write-InfoInColor "===================================================================================`n" -Foreground 'DarkCyan'
Set-Alias ngenpsh (Join-Path ([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) ngen.exe) -Force
if ($All)
{
Write-InfoInColor "EXECUTING GLOBAL NGEN`n`n" -Foreground 'Cyan'
ngenpsh update /nologo /force
}
else
{
Write-InfoInColor "EXECUTING TARGETED NGEN`n`n" -Foreground 'Cyan'
[AppDomain]::CurrentDomain.GetAssemblies() |
ForEach-Object {
if ($_.Location)
{
$Name = (Split-Path $_.location -leaf)
if ((!($Force)) -and [System.Runtime.InteropServices.RuntimeEnvironment]::FromGlobalAccessCache($_))
{
Write-InfoInColor "[SKIPPED]" -Foreground 'Yellow' -NoNewLine
Write-InfoInColor " :: " -Foreground 'White' -NoNewline
Write-InfoInColor "[ $Name ]" -Foreground 'Cyan'
}
else
{
ngenpsh install $_.location /nologo | ForEach-Object {
if ($?)
{
Write-InfoInColor "[SUCCESS]" -Foreground 'Green' -NoNewLine
Write-InfoInColor " :: " -Foreground 'White' -NoNewline
Write-InfoInColor "[ $Name ]" -Foreground 'Cyan'
}
else
{
Write-InfoInColor "[FAILURE]" -Foreground 'Red' -NoNewLine
Write-InfoInColor " :: " -Foreground 'White' -NoNewline
Write-InfoInColor "[ $Name ]" -Foreground 'Cyan'
}
}
}
}
}
}
Write-InfoInColor "`n===================================================================================" -Foreground 'DarkCyan'
Write-InfoInColor " COMPLETED NGEN " -Foreground 'Cyan'
Write-InfoInColor "===================================================================================`n" -Foreground 'DarkCyan'
}
Export-ModuleMember -Function Invoke-NGENposh