2016-09-06 29 views
1

我寫的應只安裝了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 

我想用Expand-Archive可用在PowerShell中5.0。由於實現是在.NET Framework中完成的,因此應該可以加載System.IO.Compression程序集並調用該方法。如何在程序集中訪問這些類並在PowerShell 2.0中獲得Expand-Archive

+0

你不知道。您至少需要v5才能使用'Expand-Archive',至少v3使用'System.IO.Compression.ZipFile',否則您將嘗試使用Windows Shell進行解壓縮。 – Nkosi

回答

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