8

I need some help in putting my thoughts together in a working code.

This is what I have:

1st Step: I am getting the FTP user name and password as params.

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

I then set these variables:

$FTPServer = "ftp.servername.com"
$SetType = "bin"

Now, I want to establish a connection. I Google'd for Syntax and found this. Not sure if this will establish a FTP connection. I am yet to test,

$webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPassword) 

This is the part I do not know how to code:

$Version is one of my input parameter. I have a zip file in FTP as:

ftp.servername.com\builds\my builds\$Version\Client\Client.zip

I want to download that Client.zip into my local machine's (where the script is run from) "C:\myApp\$Version" folder. So, the FTP download will create a new sub-folder with the $version name with in C:\myApp for every run.

Once this is done, I also need to know how to unzip this client.zip file under C:\myApp\$Version\Client\<content of the zip file will be here>

Bassie
  • 9,529
  • 8
  • 68
  • 159
Bhavani Kannan
  • 1,269
  • 10
  • 29
  • 46
  • 2
    Unzip: http://stackoverflow.com/questions/27768303/how-to-unzip-a-file-in-powershell or https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.archive/expand-archive . I would also consider looking into the WinSCP Assembly for Powershell https://winscp.net/eng/docs/library_powershell – dbso Jan 27 '17 at 14:35

2 Answers2

15

You may use the Expand-Archive cmdlet. They are available in Powershell version 5. Not sure with previous versions. See syntax below:

Expand-Archive $zipFile -DestinationPath $targetDir -Force

The -Force parameter will force to overwrite the files in target directory if it exists.

With your parameters, it will look like this:

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

alltej
  • 6,787
  • 10
  • 46
  • 87
2
Add-Type -assembly "System.IO.Compression.Filesystem";
[String]$Source = #pathA ;
[String]$Destination = #pathB ;
[IO.Compression.Zipfile]::ExtractToDirectory($Source, $Destination);

or

[IO.Compression.Zipfile]::CreateFromDirectory($Source,$Destination);

Depending on if you are trying to zip OR unzip.

Andrew Sun
  • 4,101
  • 6
  • 36
  • 53