Windows Server 2012中PowerShell解压Zip文件报错求助
Windows Server 2012下PowerShell解压Zip文件失败的解决方案
问题背景
在Windows Server 2012上运行PowerShell脚本解压ApplicareSingleAgentWindows.zip时,遭遇以下问题:
- 调用
Microsoft.PowerShell.Archive\Expand-Archive提示模块无法加载 - 执行
Import-Module Microsoft.PowerShell.Archive返回“找不到有效模块文件” - 尝试
Install-Module -Name Microsoft.PowerShell.Archive -RequiredVersion 1.2.5时,提示Install-Module未被识别
报错详情
解压命令报错
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
模块加载报错
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
安装模块报错
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
解决方案
1. 升级PowerShell到5.1版本
Windows Server 2012默认搭载PowerShell 3.0,Microsoft.PowerShell.Archive模块和Install-Module cmdlet仅在PowerShell 5.0及以上版本中提供。
- 获取Windows Management Framework 5.1安装包并完成安装
- 重启服务器后,重新运行
Import-Module Microsoft.PowerShell.Archive即可加载模块,原脚本的解压命令可正常执行。
2. 用.NET类替代解压(无需升级)
直接调用.NET内置的压缩类实现解压,修改脚本中的解压代码:
原代码段:
echo "Extracting ApplicareSingleagent zip file to ApplicareSingleAgentWindows directory" Microsoft.PowerShell.Archive\Expand-Archive -Path ApplicareSingleAgentWindows.Zip -DestinationPath "$scriptPath\ApplicareSingleAgentWindows" -Force
替换为:
echo "Extracting ApplicareSingleagent zip file to ApplicareSingleAgentWindows directory" # 加载.NET压缩组件 Add-Type -AssemblyName System.IO.Compression.FileSystem # 执行解压,第三个参数$true表示覆盖已有文件 [System.IO.Compression.ZipFile]::ExtractToDirectory("$pwd\ApplicareSingleAgentWindows.Zip", "$scriptPath\ApplicareSingleAgentWindows", $true)
3. 使用第三方工具7-Zip解压
若上述方法无法实施,可借助7-Zip工具:
- 在服务器上安装7-Zip(默认路径为
C:\Program Files\7-Zip\7z.exe) - 修改脚本中的解压部分为:
echo "Extracting ApplicareSingleagent zip file to ApplicareSingleAgentWindows directory" & "C:\Program Files\7-Zip\7z.exe" x "$pwd\ApplicareSingleAgentWindows.Zip" -o"$scriptPath\ApplicareSingleAgentWindows" -y
参数说明:
x:执行解压操作-o:指定解压目标目录(注意目录路径与参数间无空格)-y:自动确认覆盖已有文件
内容的提问来源于stack exchange,提问作者anandarc
相关产品推荐
相关产品推荐

