PoshCode Archive  Artifact [9c514f93ad]

Artifact 9c514f93ad796e8254a5421179498e6e738d71b570c3e1bb7326d90855cbceed:

  • File Run-NET-optimizations.ps1 — part of check-in [4a64597957] at 2018-06-10 14:04:53 on branch trunk — Force .NET Framework optimization to happen (causes high CPU by mscorsvw.exe) (user: Scott Copus size: 1744)

# encoding: ascii
# api: powershell
# title: Run .NET optimizations
# description: Force .NET Framework optimization to happen (causes high CPU by mscorsvw.exe)
# version: 0.1
# author: Scott Copus
# license: CC0
# x-poshcode-id: 5959
# x-archived: 2016-05-17T16:56:38
# x-published: 2016-08-01T14:47:00
#
# By default the optimization only uses a single CPU core.
# Running “NGEN.EXE executeQueuedItems” forces optimization to happen on all cores.
# http://blogs.msdn.com/b/dotnet/archive/2013/08/06/wondering-why-mscorsvw-exe-has-high-cpu-usage-you-can-speed-it-up.aspx
#
# Force .NET Framework optimization to happen (causes high CPU by mscorsvw.exe)
# By default the optimization only uses a single CPU core.
# Running "NGEN.EXE executeQueuedItems" forces optimization to happen on all cores.
# http://blogs.msdn.com/b/dotnet/archive/2013/08/06/wondering-why-mscorsvw-exe-has-high-cpu-usage-you-can-speed-it-up.aspx

Remove-Item variable:frameworks
# get all framework paths (adding 64-bit if necessary):
$frameworks = @("$env:SystemRoot\Microsoft.NET\Framework")
If (Test-Path "$env:SystemRoot\Microsoft.NET\Framework64") {
    $frameworks += "$env:SystemRoot\Microsoft.NET\Framework64"
}

ForEach ($framework in $frameworks) {
    # find the latest version of NGEN.EXE in the current framework path:
    $ngen_path = Join-Path (Join-Path $framework -childPath (gci $framework | where { ($_.PSIsContainer) -and (Test-Path (Join-Path $_.FullName -childPath "ngen.exe")) } | Sort Name -Descending | Select -First 1).Name) -childPath "ngen.exe"
    # execute the optimization command:
    Write-Host "$ngen_path executeQueuedItems"
    Start-Process $ngen_path -ArgumentList "executeQueuedItems" -Wait
}