在Server 2012 Core中使用Powershell解压缩文件

我需要用power shell解压缩文件.我看到每个人都这样做的典型方法是使用脚本自动化shell.
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.copyHere($zipPackage.Items())

这对我来说不起作用,因为Server Core没有shell,所以没有一个可以自动化.这会产生E_FAIL COM错误.

Powershell似乎无法独立完成,如果我参加第三方,我必须找到一种方法来首先将实用程序放到服务器上. 7-Zip是我的首选,但似乎我不能下载和安装它的脚本. Sourceforge不断向我吐出HTML文件.

如何在Server 2012 Core中完全解压缩zip文件

Server 2012附带Dot.NET 4.5,它有 System.IO.Compression.ZipFile,它有一个ExtractToDirectory方法.您应该能够在PowerShell中使用它.

这是一个例子.

首先,您需要加载ZipFile所在的程序集:

[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null

然后提取内容

[System.IO.Compression.ZipFile]::ExtractToDirectory($pathToZip,$targetDir)

编辑:如果您已更新到PowerShell 5(Windows Management Framework 5.0),则最终具有本机cmdlet:

Expand-Archive $pathToZip $targetDir

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...