加载程序集以使用Expand-Archive

时间:2016-09-06 05:46:24

标签: powershell-v2.0

我编写的脚本应该只在安装了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

1 个答案:

答案 0 :(得分:0)

你不是。您至少需要PowerShell-v5才能使用Expand-Archive或至少使用PowerShell-v3来使用.Net Framework和System.IO.Compression.ZipFile其他方面,您将尝试使用Windows shell解压缩。

在遇到使用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!'
        }
    }
}