OK999
OK999

Reputation: 1399

Failing to parameterize a download url in powershell script

I am trying to download artifacts from a nexus server. So i forged the below ps script.

$Webclient = New-Object System.Net.WebClient
$Nexusurl  = "http://ec2-54-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:8081/nexus/content/repositories/releases/org/bar" + "/" + ${BUILD_NUMBER} + "/" + ${ProjectName}.zip
$Webclient.DownloadFile($Nexusurl, 'c:\webcontent.zip')

I have 2 things as parameter in the PS. The jenkin's BUILD_NUMBER and the ProjectName. These 2 are set as a parameter in the jenkins job. The ProjectName is the name of the artifact on nexus server which is to be downloaded.

The command executes without an error. But the file c:\webcontent.zip is showing as invalid, while trying to decompress. So, i think the URL ($Nexusurl) is constructed incorrectly while executing the PS. Adding an echo like this

echo $Nexusurl

revealed that the URL is missing the parameters. They are not substituted in the $Nexusurl

http://ec2-54-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:8081/nexus/content/repositories/releases/org/bar//

What am i doing incorrect that the parameters are not substituted in the URL. I have tried a lot of combinations but unsuccessful so far!

Upvotes: 0

Views: 336

Answers (2)

TheMadTechnician
TheMadTechnician

Reputation: 36332

I would suggest using a formatted string to define $Nexusurl as such:

$Nexusurl  = "http://ec2-54-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:8081/nexus/content/repositories/releases/org/bar/{0}/{1}.zip" -f $ENV:BUILD_NUMBER, $ENV:ProjectName

This will sub in the first argument where {0} is, and the second argument where {1} is.

Upvotes: 2

OK999
OK999

Reputation: 1399

One more push and i think i am there. The final PS script looks like this:

$Webclient = New-Object System.Net.WebClient
$Nexusurl  = "http://ec2-54-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:8081/nexus/content/repositories/releases/org/bar" + "/" + $ENV:BUILD_NUMBER + "/" + $ENV:ProjectName
echo $Nexusurl
$Webclient.DownloadFile($Nexusurl, 'c:\webcontent.zip')

But still not sure why i can place an extension of .zip after the $ENV:ProjectName .. So, for the time being, i am using the parameter with the .zip extention

Upvotes: 0

Related Questions