如何使用PowerShell删除GAC中已存在的给定路径中的所有dll?

问题描述 投票:2回答:1

这里有没有PowerShell专家知道如何从GAC中已存在的给定路径中删除所有dll?

powershell powershell-v3.0 gac
1个回答
1
投票

您可以通过名称确定程序集是否已在GAC中:

$AssemblyName = [System.Reflection.AssemblyName]::GetAssemblyName("C:\Path\to\assembly.dll")
$IsInGAC = [System.Reflection.Assembly]::ReflectionOnlyLoad($AssemblyName).GlobalAssemblyCache

您可以将其包装在测试函数中以过滤输入程序集:

function Test-GACPresence {
    param(
        [Parameter(Mandatory=$true,ParameterSetName='Path')]
        [string]$Path,

        [Parameter(Mandatory=$true,ParameterSetName='LiteralPath',ValueFromPipelineByPropertyName=$true)]
        [Alias('PsPath')]
        [string]$LiteralPath
    ) 

    $LiteralPath = if($PSCmdlet.ParameterSetName -eq 'Path'){
        (Resolve-Path $Path).ProviderPath
    } else {
        (Resolve-Path $LiteralPath).ProviderPath
    }

    try{
        return [System.Reflection.Assembly]::ReflectionOnlyLoad([System.Reflection.AssemblyName]::GetAssemblyName($LiteralPath)).GlobalAssemblyCache
    }
    catch{
        return $false
    }
}

$ExistsInGAC = Get-ChildItem "path\to\test" -Filter *.dll -Recurse |?{$_|Test-GACPresence}
$ExistsInGAC |Remove-Item 
© www.soinside.com 2019 - 2024. All rights reserved.