Created
December 1, 2023 19:05
-
-
Save luke-beep/5c01a0ea9c77fd9142a020bab787d0d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Optimize-Assemblies { | |
param ( | |
[string]$assemblyFilter = "Microsoft.PowerShell.", | |
[string]$activity = "Native Image Installation" | |
) | |
try { | |
$ngenPath = Join-Path ([Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) "ngen.exe" | |
if (-Not (Test-Path $ngenPath)) { | |
Write-Host "Error: ngen.exe not found at $ngenPath. Ensure .NET Framework is installed." | |
return | |
} | |
$assemblies = [AppDomain]::CurrentDomain.GetAssemblies() | | |
Where-Object { $_.FullName -ilike "$assemblyFilter*" } | |
if ($assemblies.Count -eq 0) { | |
Write-Host "No assemblies found matching filter: $assemblyFilter." | |
return | |
} | |
foreach ($assembly in $assemblies) { | |
$assemblyName = [System.IO.Path]::GetFileName($assembly.Location) | |
Write-Progress -Activity $activity -Status "Optimizing $assemblyName" | |
Start-Process -FilePath $ngenPath -ArgumentList "install `"$($assembly.Location)`"" -Wait -NoNewWindow | |
} | |
Write-Host "All specified assemblies have been optimized." | |
} catch { | |
Write-Host "An error occurred: $_" | |
} | |
} | |
Optimize-Assemblies -assemblyFilter "Microsoft.PowerShell." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment