Introduction
Recently at my customer’s site, I started using XebiaLabs product called XL Deploy. It is a software solution that helps automating the deployment process. Many operations are exposed via a REST API. Uploading of packages is one of them and is done following the Multipart/form-data standard. In case you need to do so, via PowerShell, you may be surprised about the difficulty of something that appears to be a trivial task. As you may know, PowerShell doesn’t play well with Multipart/form-data requests. I will show you a cmdlet that I wrote that can be handy in accomplishing this task.
Welcome Send-Package cmdlet
Before showing you the actual cmdlet that will send a package to XL Deploy I need to mention that the core of this cmdlet is Invoke-MultipartFormDataUpload cmdlet about which I blogged earlier.
First of all two small helper cmdlets. As the file name needs to be specified in the URL, we need to encode it. This may be done with some simpler code for this particular case, however I will show you a cmdlet that I’m using also for other XL Deploy calls where this approach is necessary.
<############################################################################################ Encodes each part of the path separately. ############################################################################################> function Get-EncodedPathParts() { [CmdletBinding()] param ( [string][parameter(Mandatory = $true)]$PartialPath ) BEGIN { } PROCESS { return ($PartialPath -split "/" | %{ [System.Uri]::EscapeDataString($_) }) -join "/" } END { } }
The other one will make sure that the URL passed in is formatted correctly for XL Deploy and that is a valid URL.
<############################################################################################ Verifies that the endpoint URL is well formatted and a valid URL. ############################################################################################> function Test-EndpointBaseUrl() { [CmdletBinding()] param ( [Uri][parameter(Mandatory = $true)]$Endpoint ) BEGIN { Write-Verbose "Endpoint = $Endpoint" } PROCESS { #$xldServer = $serviceEndpoint.Url.AbsoluteUri.TrimEnd('/') $xldServer = $Endpoint.AbsoluteUri.TrimEnd('/') if (-not $xldServer.EndsWith("deployit", "InvariantCultureIgnoreCase")) { $xldServer = "$xldServer/deployit" } # takes in consideration both http and https protocol if (-not $xldServer.StartsWith("http", "InvariantCultureIgnoreCase")) { $xldServer = "http://$xldServer" } $uri = $xldServer -as [System.Uri] if (-not ($uri.AbsoluteURI -ne $null -and $uri.Scheme -match '[http|https]')) { throw "Provided endpoint address is not a valid URL." } return $uri } END { } }
This cmdlets and the one I described in the previous post are required to be available for our new upload cmdlet. After having assured that they are available we can write a cmdlet I named Send-Package.
<############################################################################################ Uploads the given package to XL Deploy server. ############################################################################################> function Send-Package() { [CmdletBinding()] PARAM ( [string][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]$PackagePath, [Uri][parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]$EndpointUrl, [System.Management.Automation.PSCredential]$Credential ) BEGIN { if (-not (Test-Path $PackagePath)) { $errorMessage = ("Package {0} missing or unable to read." -f $PackagePath) $exception = New-Object System.Exception $errorMessage $errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, 'SendPackage', ([System.Management.Automation.ErrorCategory]::InvalidArgument), $PackagePath $PSCmdlet.ThrowTerminatingError($errorRecord) } $EndpointUrl = Test-EndpointBaseUrl $EndpointUrl } PROCESS { $fileName = Split-Path $packagePath -leaf $fileName = Get-EncodedPathParts($fileName) $uri = "$EndpointUrl/package/upload/$fileName" $response = Invoke-MultipartFormDataUpload -InFile $PackagePath -Uri $uri -Credential $credentials return ([xml]$response).'udm.DeploymentPackage'.id } END { } }
You can now invoke this cmdlet and pass the requested parameters. If the call succeeds you will get back the package id.
At example:
$uri = "http://xld.majcica.com:4516" $filePath = "C:\Users\majcicam\Desktop\package.dar" $credentials = (Get-Credential) $packageId = Send-Package $filePath $uri $credentials Write-Output "Package was uploaded successfully with the following id: '$packageId'"
In case you do not want to provide credentials interactively, the following cmdlet may help you:
<############################################################################################ Given the username and password strings, create a valid PSCredential object. ############################################################################################> function New-PSCredential() { [CmdletBinding()] param ( [string][parameter(Mandatory = $true)]$Username, [string][parameter(Mandatory = $true)]$Password ) BEGIN { Write-Verbose "Username = $Username" } PROCESS { $securePassword = ConvertTo-SecureString –String $Password -asPlainText -Force $credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $Username, $securePassword return $credential } END { } }
That’s all folks! I tested this scripts with XL Deploy 5.1.0 and 5.1.3 and had not encountered any issues. If any, do not hesitate to comment.
Happy deploying!
Wow, great blog Mario! Thanks for sharing in such a collaborative way. I hope you enjoy deploying with XL Deploy and we are all looking forward to more XLD tips in the future : )
Hi, thank you for your kind words! I have planned at least two new posts, to publish in the upcoming weeks, treating XL Deploy. Stay tuned! 🙂