【问题标题】:powershell script to check debug mode of dll in specific folderpowershell脚本检查特定文件夹中dll的调试模式
【发布时间】:2023-04-08 11:40:01
【问题描述】:

我希望使用 powershell 脚本在当前文件夹中以调试模式列出 DLL。 是否可以这样做,请您帮助准备脚本。

谢谢@Moerwald。根据您提供的脚本和参考,已将其扩展为递归运行给定文件夹中的所有 DLL。你可以看看它。

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}                   
    } 

【问题讨论】:

    标签: .net powershell debugging dll


    【解决方案1】:

    基于此stackoverflow answer:

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

    如果程序集在调试模式下编译,您会得到如下结果:

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

    对于发布模式:

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

    简单的功能(没有错误检查):

    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 {
    }
    

    }

    希望对您有所帮助。

    【讨论】:

    • 谢谢莫尔瓦尔德。您能否解释以下行: $debugAttribute[0].IsJITOptimizerDisabled -eq $false -and $debugAttribute[0].IsJITTrackingEnabled -eq $false
    • 抱歉,我们应该检查一下$true。在调试版本中,IsJITOptimizerDisabled 和 IsJITTrackingEnabled 设置为 $true。由于-and 操作数的结果未存储在变量中,因此将结果写入 PowerShell 输出流(或多或少等同于 $result = ....; return $result)。检查以下链接以获取流解释 -> blogs.technet.microsoft.com/heyscriptingguy/2014/03/30/…
    • 感谢@Moerwald,根据您的代码和解释,编写了代码以递归地扫描文件夹中的 DLL,并对其进行了更新。请看一看。谢谢
    • @DurgaprasadPotnuru:如果你想要单线,你可以选择: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 的帮助,我已将您的回复标记为答案。
    猜你喜欢
    • 1970-01-01
    • 2023-01-21
    • 2014-03-02
    • 1970-01-01
    • 2022-08-05
    • 2011-03-26
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多