programing

현재 디렉터리 및 모든 하위 디렉터리에 있는 DLL 파일의 파일 버전 및 어셈블리 버전 가져오기

starjava 2023. 7. 31. 20:59
반응형

현재 디렉터리 및 모든 하위 디렉터리에 있는 DLL 파일의 파일 버전 및 어셈블리 버전 가져오기

디렉토리와 그 하위 디렉토리에 있는 모든 DLL 파일의 파일 버전과 어셈블리 버전을 가져올 수 있으면 좋겠습니다.저는 프로그래밍을 처음 하는데, 이 루프가 어떻게 작동하는지 모르겠어요.

어셈블리 버전을 가져올 PowerShell 코드가 있습니다(포럼에서 가져온 것).

$strPath = 'c:\ADMLibrary.dll'
$Assembly = [Reflection.Assembly]::Loadfile($strPath)

$AssemblyName = $Assembly.GetName()
$Assemblyversion = $AssemblyName.version

그리고 이것 또한:

$file = Get-ChildItem -recurse | %{ $_.VersionInfo }

디렉토리에 있는 모든 파일의 어셈블리 버전을 반환하려면 어떻게 루프를 만들 수 있습니까?

여기 예쁜 라이너가 있습니다.

Get-ChildItem -Filter *.dll -Recurse | Select-Object -ExpandProperty VersionInfo

PowerShell 버전 2의 약어는 다음과 같습니다.

ls -fi *.dll -r | % { $_.versioninfo }

tamasf에서 제안한 PowerShell 버전 3의 약어는 다음과 같습니다.

ls *.dll -r | % versioninfo

못생긴 원라이너로서:

Get-ChildItem -Filter *.dll -Recurse |
    ForEach-Object {
        try {
            $_ | Add-Member NoteProperty FileVersion ($_.VersionInfo.FileVersion)
            $_ | Add-Member NoteProperty AssemblyVersion (
                [Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version
            )
        } catch {}
        $_
    } |
    Select-Object Name,FileVersion,AssemblyVersion

현재 디렉토리만 원하는 경우에는 다음을 제외합니다.-Recurse매개 변수DLL만 사용하지 않고 모든 파일을 사용하려는 경우-Filter매개 변수 및 해당 인수.코드는 (바라건대) 꽤 간단합니다.

그 안에 있는 불쾌한 부분들을 분리하는 것이 좋습니다.try여기서 오류 처리를 덜 어색하게 만들기 때문에 별도의 기능으로 차단합니다.

샘플 출력:

Name                                    FileVersion     AssemblyVersion
----                                    -----------     ---------------
Properties.Resources.Designer.cs.dll    0.0.0.0         0.0.0.0
My Project.Resources.Designer.vb.dll    0.0.0.0         0.0.0.0
WindowsFormsControlLibrary1.dll         1.0.0.0         1.0.0.0
WindowsFormsControlLibrary1.dll         1.0.0.0         1.0.0.0
WindowsFormsControlLibrary1.dll         1.0.0.0         1.0.0.0

선택 - 객체가 속성을 생성하도록 합니다.

Get-ChildItem -Filter *.dll -Recurse | Select-Object Name,@{n='FileVersion';e={$_.VersionInfo.FileVersion}},@{n='AssemblyVersion';e={[Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version}}

그리고 샘플 출력도 비슷합니다.

Name                                           FileVersion AssemblyVersion
----                                           ----------- ---------------
CI.EntityFramework.Initialization.dll          1.0.0.0     1.0.0.0
Castle.Core.dll                                3.3.0.43    3.3.0.0
Castle.Windsor.dll                             3.3.0.51    3.3.0.0
Mutare.VitalLink.dll                           1.0.0.0     1.0.0.0
Newtonsoft.Json.dll                            9.0.1.19813 9.0.0.0

여기 꽤 괜찮은 한 줄기가 있습니다.

Get-ChildItem -Filter *.dll -Recurse | ForEach-Object `
{
    return [PSCustomObject]@{
        Name = $_.Name
        FileVersion = $_.VersionInfo.FileVersion
        AssemblyVersion = ([Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version)
    }
}

샘플 출력:

Name            FileVersion AssemblyVersion
----            ----------- ---------------
Minimatch.dll   1.1.0.0     1.1.0.0
VstsTaskSdk.dll 1.0.0.0     1.0.0.0

Joey의 답변을 기반으로 하지만 암묵적인 예외 처리를 위해 유용한 행동을 이용합니다.먼저 확장 속성을 추가합니다.

Update-TypeData -TypeName System.IO.FileInfo -MemberType ScriptProperty -MemberName AssemblyVersion -Value { [Reflection.AssemblyName]::GetAssemblyName($this.FullName).Version }

선택적으로 재사용할 수 있도록 프로필에 저장할 수 있습니다.그러면 실제 선택 항목은 예를 들어,

Get-ChildItem -Filter *.dll -Recurse | Select-Object Name,AssemblyVersion

참고로, 제가 이것을 추가적인 답변으로 게시하는 주된 이유는 저와 같은 PowerShell 초보자들의 이익을 위해서입니다. Joey의 답변이 에 주어진 정의로 바뀌어야 한다는 것을 깨닫는오랜 시간이 걸렸습니다.

$j = 'C:\Program Files\MySQL\Connector ODBC 8.0\' # this is the path of foler where you want check your dlls 
$files = get-childitem $j -recurse -include *.dll # this is the command thatwill check all the dlls in that folder 

foreach ($i in $files) {
   $verison = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($i).FileVersion
   Write-Host  "$i ----> $verison "
} # loop is used where it will travel throuhg all the files of the specified folder and check the verion and will print it 

언급URL : https://stackoverflow.com/questions/3267009/get-file-version-and-assembly-version-of-dll-files-in-the-current-directory-and

반응형