使用PowerShell脚本下载zip文件并解压缩

Bha*_*nan 3 powershell zip powershell-2.0

我需要一些帮助,以将我的想法整合到一个有效的代码中。

这就是我所拥有的:

第一步:我将FTP用户名和密码作为参数。

param(#define parameters
[Parameter(Position=0,Mandatory=$true)]
    [string]$FTPUser
[Parameter(Position=1,Mandatory=$true)]
    [string]$FTPPassword    
[Parameter(Position=2,Mandatory=$true)]
    [string]$Version    
)
Run Code Online (Sandbox Code Playgroud)

然后设置以下变量:

$FTPServer = "ftp.servername.com"
$SetType = "bin"
Run Code Online (Sandbox Code Playgroud)

现在,我想建立一个连接。我用Google搜索语法,发现了这一点。不确定是否会建立FTP连接。我还没有测试

$webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPassword) 
Run Code Online (Sandbox Code Playgroud)

这是我不知道如何编码的部分:

$Version是我的输入参数之一。我在FTP中有一个zip文件,如下所示:

ftp.servername.com\builds\my builds\$Version\Client\Client.zip
Run Code Online (Sandbox Code Playgroud)

我想将该Client.zip下载到本地计算机的(从中运行脚本的)"C:\myApp\$Version"文件夹中。因此,FTP下载将为每次运行创建一个$version名为in 的新子文件夹C:\myApp

完成此操作后,我还需要知道如何将以下client.zip文件解压缩到 C:\myApp\$Version\Client\<content of the zip file will be here>

all*_*tej 5

您可以使用Expand-Archivecmdlet。它们在Powershell版本5中可用。不确定以前的版本。请参见下面的语法:

Expand-Archive $zipFile -DestinationPath $targetDir -Force

-Force参数将强制覆盖目标目录中的文件(如果存在)。

使用您的参数,它将如下所示:

Expand-Archive "C:\myApp\$Version\Client.zip" -DestinationPath "C:\myApp\$Version" -Force


小智 2

Add-Type -assembly "System.IO.Compression.Filesystem";
[String]$Source = #pathA ;
[String]$Destination = #pathB ;
[IO.Compression.Zipfile]::ExtractToDirectory($Source, $Destination);
Run Code Online (Sandbox Code Playgroud)

或者

[IO.Compression.Zipfile]::CreateFromDirectory($Source,$Destination);
Run Code Online (Sandbox Code Playgroud)

取决于您是否尝试压缩或解压缩。