Module/Administration/Get-FileFromURLOnRemote.ps1
<#
.SYNOPSIS Download a file on a remote computer .DESCRIPTION Download a file on a remote computer .PARAMETER remoteServerName Remote server where the file is to be downloaded .PARAMETER sourceURL Source URL to the file .PARAMETER destinationFolder Destination folder on the remote server. .PARAMETER credentials Your credentials to the remote server .NOTES Author: Mathias Stjernfelt Website: http://www.brightcom.se .EXAMPLE Get-FileFromURLOnRemote -remoteServer "myserver" -sourceURL "http://myurl/myfile.ext" -destination "c:\myfolder" -credentials (Get-BCSCredential -userName myUserName -securePassword (Get-BCSSecureString "myPassword")) #> function Get-FileFromURLOnRemote { Param ( [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)] $remoteServerName, [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)] $sourceURL, [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)] $destinationFolder, [Parameter(ValueFromPipelineByPropertyName, Mandatory = $true)] [System.Management.Automation.PSCredential]$credentials ) begin {} process { $ScriptBlock = { Param ( $sourceURL, $destinationFolder ) if ($sourceURL -like "http://*" -or $sourceURL -like "https://*") { if (-not(Test-Path -Path $destinationFolder)) { New-Item -Type Directory -Path $destinationFolder | Out-Null } $urlWithoutQuery = $sourceURL.Split('?')[0] $sourceFilename = Split-Path -Path $urlWithoutQuery -Leaf $destinationFolder = "$destinationFolder\$sourceFilename" Write-Host "Downloading file $urlWithoutQuery to $destinationFolder" (New-Object System.Net.WebClient).DownloadFile($sourceURL, $destinationFolder) } else { Write-Error "$sourceURL is not a valid URL" } } try { $session = New-PSSession -UseSSL -ComputerName $remoteServerName -Credential $credentials -ErrorAction SilentlyContinue Invoke-Command -Session $session -ScriptBlock $ScriptBlock -ArgumentList ($sourceURL, $destinationFolder) Remove-PSSession -Session $session } catch { Remove-PSSession -Session $session Write-Error $_.Exception.Message } } end { Remove-PSSession -Session $session } } Export-ModuleMember -Function Get-FileFromURLOnRemote |