Andrej Pit
Andrej Pit

Reputation: 3

Copy latest file from server to local destination

I've been struggling with a script that needs to copy or download the latest file from a file server to the local drive folder. Here is what i came up with:

[String]$LocalServer = "\\IP_address\D:\Interfaces\"             
[String]$File_Name = "MK." + (Get-Date).ToString("ddMMyy") + ".tar.gz"  #file name is example MK.040418.tar.gz  
[String]$Path = "C:\Localfolder"                               
[String]$Username = "admin"
[String]$Password = "Admin123"

Copy-Item -Path {$LocalServer + "\" + $FileName} - Destination {$LocalPath}

#$WebClient = New-Object System.Net.WebClient
#$WebClient.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
#$WebClient.DownloadFile($LocalServer, $FileName)

Upvotes: 0

Views: 38

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13217

There's a few issues with your code:

You don't need to use {} to wrap parameters with Powershell.

Your variable is $File_Name but you're using $FileName (missing _) with Copy-Item command.

$LocalServer ends with a \, and you're also adding one in with$LocalServer + "\" + $FileName, so the path ends up having a double slash: \\IP_address\D:\Interfaces\\MK.040418.tar.gz

Fixing these points, the command should be:

Copy-Item -Path "$LocalServer$File_Name" -Destination $LocalPath

Upvotes: 1

Related Questions