0

I am looking to list out DLLs in debug mode in current folder using powershell script. is it possible to do so, can you please help in preparing the script.

thanks you @Moerwald. based on your script and reference provided, have expanded it to run for all DLLs in given folder recursively. you may have a look at it please.

Get-ChildItem -Filter *.dll -Recurse |
    ForEach-Object {
        $AssemblyName= $_.FullName;      
        try {
         $Assembly = [Reflection.Assembly]::LoadFile($AssemblyName);
         $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{ "***ERROR*** Error when loading assembly: " + $AssemblyName + $_.Exception.Message}                   
    } 

1 Answers1

0

Based on this stackoverflow answer:

$type = [System.Type]::GetType("System.Diagnostics.DebuggableAttribute")
$path = "$($($pwd).Path)\xyz.dll"
$loadedAss = [System.Reflection.Assembly]::LoadFile($path)
$loadAss.GetCustomAttributes($type, $false)

If the assembly is compiled in debug mode you get a result like:

IsJITTrackingEnabled   : True
IsJITOptimizerDisabled : True
DebuggingFlags         : Default, IgnoreSymbolStoreSequencePoints, EnableEditAndContinue, DisableOptimizations
TypeId                 : System.Diagnostics.DebuggableAttribute

For release mode:

IsJITTrackingEnabled   : False
IsJITOptimizerDisabled : False
DebuggingFlags         : IgnoreSymbolStoreSequencePoints
TypeId                 : System.Diagnostics.DebuggableAttribute

Simple function (without error checking):

function Is-AssemblyDebugOne {
[CmdletBinding()]
param (
    [ValidateScript({Test-Path $_ })] 
    [string]
    $absolutePathToAssembly
)

begin {
    $type = [System.Type]::GetType("System.Diagnostics.DebuggableAttribute")
}

process {
    $loadedAss = [System.Reflection.Assembly]::LoadFile($absolutePathToAssembly)
    $debugAttribute = $loadedAss.GetCustomAttributes($type, $false)
    $debugAttribute[0].IsJITOptimizerDisabled -eq $true -and $debugAttribute[0].IsJITTrackingEnabled -eq $true
}

end {
}

}

Hope that helps.

Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • Thanks Moerwald. Could you explain following line: $debugAttribute[0].IsJITOptimizerDisabled -eq $false -and $debugAttribute[0].IsJITTrackingEnabled -eq $false – Durgaprasad Potnuru Jul 10 '18 at 09:03
  • Sorry, we should check against `$true`. In debug build `IsJITOptimizerDisabled ` and `IsJITTrackingEnabled ` are set to `$true`. Since the result of the `-and` operand is not stored in a variable, the result is written to the PowerShell output-stream (which is more or less equivalent to $result = ....; return $result). Check the following link for stream explanation -> https://blogs.technet.microsoft.com/heyscriptingguy/2014/03/30/understanding-streams-redirection-and-write-host-in-powershell/ – Moerwald Jul 10 '18 at 09:29
  • thanks @Moerwald , based on your code and explanation have written code to scan the DLLs recursively in a folder, updated it in question. please have a look. thanks – Durgaprasad Potnuru Jul 11 '18 at 10:27
  • @DurgaprasadPotnuru: If you want a one-liner you can go with: `Get-ChildItem . -Recurse *.dll | % -Begin {$type = [System.Type]::GetType("System.Diagnostics.DebuggableAttribute");} -Process { $debugAttribute = $([System.Reflection.Assembly]::LoadFile($_.FullName)); $props = @{ attributes = $debugAttribute.GetCustomAttributes($type, $false); name = $_.Name} ; New-Object -TypeName psobject -Property $props} | ? { $_.attributes[0].IsJITOptimizerDisabled -eq $true } | select name -expandproperty` – Moerwald Jul 13 '18 at 05:55
  • I will try it out and let you know. Thanks for the help Moerwald, I have marked your reply as answer. – Durgaprasad Potnuru Jul 14 '18 at 08:23