如何使用PowerShell脚本在Windows Server 2012上提取ZIP文件

kgsdhlau  于 2023-04-21  发布在  Shell
关注(0)|答案(1)|浏览(157)

解压压缩文件通过powershell,我使用下面的脚本。但我得到这个错误信息

Microsoft.PowerShell.Archive\Expand-Archive : The module 'Microsoft.PowerShell.Archive' could not be loaded. For more
information, run 'Import-Module Microsoft.PowerShell.Archive'.
At C:\windowsagentinstall.ps1:34 char:1
+ Microsoft.PowerShell.Archive\Expand-Archive -Path ApplicareSingleAgentWindows.Zi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...\Expand-Archive:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoLoadModule

这是我的剧本

echo "Downloading Applicare SingleAgent Installer ..."

#This will hide the progress bar. If the progress bar is enabled it will slow the download process
$ProgressPreference = 'SilentlyContinue'

Invoke-WebRequest -Uri $env:PROTOCOL"://"$env:IP":"$env:PORT/applicare/standalone?userId=$env:USERID"&"filename=ApplicareSingleAgentWindows.zip -OutFile $pwd\ApplicareSingleAgentWindows.zip

echo "Download Complete"
try {
    $scriptPath = $PSScriptRoot
    if (!$scriptPath)
    {
        if ($psISE)
        {
            $scriptPath = Split-Path -Parent -Path $psISE.CurrentFile.FullPath
        }
        else {
            Write-Host -ForegroundColor Red "Cannot resolve script file's path"
            exit 1
        }
    }
}
catch {
    Write-Host -ForegroundColor Red "Caught Exception: $($Error[0].Exception.Message)"
    exit 2
}
#echo "Current directory"
Write-Host "SingleAgent downloaded in Path: $scriptPath"

echo "Creating ApplicareSingleAgentWindows directory"
New-Item -Path 'ApplicareSingleAgentWindows' -ItemType Directory -Force

echo "Extracting ApplicareSingleagent zip file to ApplicareSingleAgentWindows directory"
Microsoft.PowerShell.Archive\Expand-Archive -Path ApplicareSingleAgentWindows.Zip -DestinationPath "$scriptPath\ApplicareSingleAgentWindows" -Force

Set-Location -Path "$scriptPath\ApplicareSingleAgentWindows"

我执行此“Import-Module Microsoft.PowerShell.Archive”命令,但它不起作用,我得到以下错误

Import-Module : The specified module 'Microsoft.PowerShell.Archive' was not loaded because no valid module file was
found in any module directory.
At line:1 char:1
+ Import-Module Microsoft.PowerShell.Archive
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (Microsoft.PowerShell.Archive:String) [Import-Module], FileNotFound
   Exception
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

我尝试了另一个命令,得到了相同的问题。“Install-Module -Name Microsoft.PowerShell.Archive -RequiredVersion 1.2.5”

PS C:\ApplicareSingleAgentWindows> Install-Module -Name Microsoft.PowerShell.Archive -RequiredVersion 1.2.5
Install-Module : The term 'Install-Module' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ Install-Module -Name Microsoft.PowerShell.Archive -RequiredVersion 1.2.5
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Install-Module:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
7fhtutme

7fhtutme1#

您声明您使用的是PowerShell 4.0

  • 看起来,它没有附带Microsoft.PowerShell.Archive模块。
  • 它也没有附带PowerShellGet模块,因此您无法使用Install-Module来安装它。

你有两个选择:

  • Either:安装PowerShellGet模块,以便使Install-Module对您可用,这使您可以安装PowerShell Gallery中可用的 * 任何 * 模块。
  • 然而,这样做并不简单,可能需要在 PowerShell v5+ 机器上使用Save-Module,您可以从该机器将下载的模块复制到v4机器。
  • 参见official instructions
    • 或 *:直接使用.NET API-此 * 假设您安装了.NET Framework 4.5或更高版本 *:
Add-Type -AssemblyName System.IO.Compression, System.IO.Compression.FileSystem

# Note: Be sure to use *full* paths, because .NET's working dir.
#       usually differs from PowerShell's.
[System.IO.Compression.ZipFile]::ExtractToDirectory(
  (Convert-Path -LiteralPath ApplicareSingleAgentWindows.Zip),
  "$scriptPath\ApplicareSingleAgentWindows",
  $true
)

相关问题