Created
June 14, 2019 02:56
-
-
Save richardszalay/59664cd302e66511618f51eaaa77db26 to your computer and use it in GitHub Desktop.
Enable/Disable JIT optimizations for assemblies so they can be debugged with dnSpy
This file contains hidden or 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
Set-StrictMode -Version Latest | |
$ErrorActionPreference = "Stop" | |
function Enable-IISAssemblyDebugging | |
{ | |
param( | |
[string]$Path = ".", | |
[string]$Filter = "*.dll" | |
) | |
$Path = Resolve-Path $Path | |
$binPath = Join-Path $Path "bin" | |
$webConfig = Join-Path $Path "Web.config" | |
if (-not (Test-Path $binPath)) { | |
throw "Path does not contain a bin\ folder" | |
} | |
if (-not (Test-Path $webConfig)) { | |
throw "Path does not contain a Web.config file" | |
} | |
$assemblies = Get-ChildItem -Path $binPath -Filter $Filter -File | |
$assemblies | Foreach-Object { | |
$assemblyIniPath = Join-Path $_.DirectoryName "$($_.BaseName).ini" | |
Set-Content -Path $assemblyIniPath -Value "[.NET Framework Debugging Control] | |
GenerateTrackingInfo=1 | |
AllowOptimize=0" | |
} | Out-Null | |
$doc = New-Object System.Xml.XmlDocument | |
$doc.Load([string]$webConfig) | |
$hostingEnvironmentEl = $doc.SelectSingleNode("/configuration/system.web/hostingEnvironment") | |
if ($hostingEnvironmentEl -eq $null) { | |
$hostingEnvironmentEl = $doc.CreateElement("hostingEnvironment") | |
$systemWebEl = $doc.SelectSingleNode("/configuration/system.web") | |
if ($systemWebEl -eq $null) { | |
$systemWebEl = $doc.CreateElement("system.web") | |
$doc.DocumentElement.AppendChild($systemWebEl) | |
} | |
$systemWebEl.AppendChild($hostingEnvironmentEl) | Out-Null | |
} | |
$hostingEnvironmentEl.SetAttribute("shadowCopyBinAssemblies", "false") | |
$doc.Save($webConfig) | |
} | |
function Disable-IISAssemblyDebugging | |
{ | |
param( | |
[string]$Path = ".", | |
[string]$Filter = "*.dll" | |
) | |
$Path = Resolve-Path $Path | |
$binPath = Join-Path $Path "bin" | |
$webConfig = Join-Path $Path "Web.config" | |
if (-not (Test-Path $binPath)) { | |
throw "Path does not contain a bin\ folder" | |
} | |
if (-not (Test-Path $webConfig)) { | |
throw "Path does not contain a Web.config file" | |
} | |
$assemblies = Get-ChildItem -Path $binPath -Filter $Filter -File | |
$assemblies | | |
Foreach-Object { Join-Path $_.DirectoryName "$($_.BaseName).ini" } | | |
Where-Object { Test-Path $_ } | | |
ForEach-Object { Remove-Item $_ } | | |
Out-Null | |
$doc = New-Object System.Xml.XmlDocument | |
$doc.Load([string]$webConfig) | |
$hostingEnvironmentEl = $doc.SelectSingleNode("/configuration/system.web/hostingEnvironment") | |
if ($hostingEnvironmentEl -ne $null) { | |
$hostingEnvironmentEl.RemoveAttribute("shadowCopyBinAssemblies") | |
if ($hostingEnvironmentEl.Attributes.Count -eq 0) { | |
$hostingEnvironmentEl.ParentNode.RemoveChild($hostingEnvironmentEl) | Out-Null | |
} | |
} | |
$doc.Save($webConfig) | |
} | |
Export-ModuleMember Disable-IISAssemblyDebugging | |
Export-ModuleMember Enable-IISAssemblyDebugging |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment