加载程序集以使用 Expand-Archive

Load assembly to use Expand-Archive

我写了一个脚本,应该 运行 在只安装了 PowerShell 2.0 的系统上:

PS C:\> $PSVersionTable

Name                           Value
----                           ----- 
CLRVersion                     2.0.50727.5485 
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

我想使用 PowerShell 5.0 中提供的 Expand-Archive。由于实现是在 .NET Framework 中完成的,因此应该可以加载 System.IO.Compression 程序集并调用该方法。如何在程序集中访问这些 类 并在 PowerShell 2.0 中获取 Expand-Archive

你不知道。您至少需要 PowerShell-v5 才能使用 Expand-Archive 或至少 PowerShell-v3 才能使用 .Net Framework 和 System.IO.Compression.ZipFile 否则您只能尝试使用 windows [=18 解压缩=].

在遇到带有 Powershell-v2 的客户端后,我最终得到了这样的功能。

<#
    .SYNOPSIS
        Extract the content of the referenced zip file to the defind destination

    .PARAMATER Path
        Path to a zip file with the file extension '.zip'

    .Parameter Destination
        Path to which the zip content is extracted
#>
Function Expand-ZipFile {
    [CmdletBinding()]
    param (
        [Parameter(Position=0, Mandatory=$true)]
        [String] $Path,

        [Parameter(Position=1, Mandatory=$true)]
        [String] $Destination
    )
    process {
        Write-Debug "Unzipping $Path to $Destination..."

        # Check if powershell v3+ and .net v4.5 is available
        $netFailed = $true
        if ( $PSVersionTable.PSVersion.Major -ge 5) { 
            try {
                Expand-Archive $Path $Destination
                $netFailed = $false
            }
            catch {
            }
        }elseif  ( $PSVersionTable.PSVersion.Major -ge 3 -and (Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4' -Recurse | Get-ItemProperty -Name Version | Where-Object { $_.Version -like '4.5*' }) ) {
            Write-Debug 'Attempting unzip using the .NET Framework...'

            try {
                [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
                [System.IO.Compression.ZipFile]::ExtractToDirectory($Path, $Destination)
                $netFailed = $false
            }
            catch {
            }
        }

        if ($netFailed) {
            try {
                Write-Debug 'Attempting unzip using the Windows Shell...'
                $shellApp = New-Object -Com Shell.Application
                $shellZip = $shellApp.NameSpace([String]$Path)
                $shellDest = $shellApp.NameSpace($Destination)
                $shellDest.CopyHere($shellZip.items())
            }
            catch {
                $shellFailed = $true
            }
        }

        # if failure already registered or no result
        if (($netFailed -and $shellFailed) -or ((Get-ChildItem $Destination | Measure-Object | Where-Object { $_.Count -eq 0}))) {
            Write-Warning 'We were unable to decompress the zip file. This tends to mean both of the following are true:'
            Write-Warning '1. You''ve disabled Windows Explorer Zip file integration or are running on Windows Server Core.'
            Write-Warning '2. You don''t have the .NET Framework 4.5 installed.'
            Write-Warning 'You''ll need to correct at least one of the above issues depending on your installation to proceed.'
            throw 'Unable to unzip file!'
        }
    }
}