Tuesday, February 26, 2019

PowerShell to check whether the .Net assembly is compiled into debug or release

Often as part of reviews we need to check whether the application deployed is compiled in release mode or not. Most of the CI/CD tools compile in release mode only. But if some has modified any assemblies post deployment manually and missed to compile in release mode, it is worth checking the assemblies.

It is easy to run PowerShell in productio environments than running a tool to check. Running an external exe is often makes discomfort to the production support guys. Essentially running a PowerShell script and custom tool are same from technical perspective. But since the PowerShell source is plain text and production support guys are familiar with PowerShell, they normally allow.

This is not my creation. Got from somewhere in the internet and posting here so I can use it next time.

Get-ChildItem -Filter *.dll -Recurse |

    ForEach-Object {

        try {
         $Assembly = [Reflection.Assembly]::LoadFile($_.FullName);
         $type = [System.Type]::GetType("System.Diagnostics.DebuggableAttribute");
         $debugAttribute = $Assembly.GetCustomAttributes($type,$false);
         If ($debugAttribute.count -Eq 0) {
            "{$_.Name} :Release"
         }
         ElseIf ($debugAttribute[0].IsJITOptimizerDisabled -eq $false) {
            "{$_.Name} :Release"
         }
         Else {
            "{$_.Name}  :Debug"
            }
        } catch{ "{$_.FullName} ***ERROR*** Error when loading assembly: " }                 

    }

Make sure the PowerShell is in the right folder context so that it can get the files using *.dll filter.

A .Net version is available in Hanselman's blog.

No comments: