Created
January 22, 2017 04:23
-
-
Save rwilkes/4a92e121f96d2c324f971aeb4380d29d to your computer and use it in GitHub Desktop.
BITS File Transfer Script Transfer file/s between file or web servers using BITS (Background Intelligent Transfer Service)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Transfer file/s between file or web servers using BITS (Background Intelligent Transfer Service) | |
.Description | |
This script transfers a file between a file or web server to another file location using BITS. | |
Parameters can specify the Source Server, the Destination Server and the Credentials for the connection. | |
By the usage of wildcard for the Source Server location multiple files can be transferred. | |
.Example | |
Start-TransferFile.ps1 -s "\\serverS\share\file.ext" -d "\\serverD\share" -c "domain\user" | |
Transfers the file "file.ext" from source server "\\serverS\share\" to destination server "\\serverD\share" | |
.Example | |
Start-TransferFile.ps1 -s "\\serverS\share\*.*" -d "\\serverD\share" | |
Transfers multiple files (*.*) from source server "\\serverS\share\" to destination server "\\serverD\share" | |
.Example | |
Start-TransferFile.ps1 -s "\\serverS\share\*.iso" -d "\\serverD\share" | |
Transfers multipe .iso files from source server "\\serverS\share\" to destination server "\\serverD\share" | |
.Example | |
Start-TransferFile.ps1 -s "http://webserverS/file.ext" -d "\\serverD\share" -c "domain\user" | |
Transfers the file "file.ext" from source web server "http://webserverS/" to destination server "\\serverD\share" | |
.Example | |
Start-TransferFile.ps1 -s "http://webserverS/file.ext" -d "\\serverD\share" -c "domain1\user1" | |
Transfers the file "file.ext" from source web server "http://webserverS/" to destination server "\\serverD\share" using the credentials domain1\user1 | |
.Inputs | |
SourcePath - Mandatory Parameter | |
DestinationPath | |
Credentials | |
.OutPuts | |
N/A | |
.Notes | |
NAME: StartTransferFile.ps1 | |
AUTHOR: Benjamin R Wilkinson | |
VERSION: 2.0.0 | |
LASTEDIT: 04/10/2010 | |
KEYWORDS: BITS, BitsTransfer, Start-BitsTransfer, Complete-BitsTransfer, | |
Remove-BitsTransfer, Get-BitsTransfer, get-service | |
.Link | |
http://msdn.microsoft.com/en-us/library/aa363160(VS.85).aspx | |
http://msdn.microsoft.com/en-us/library/aa362783(v=VS.85).aspx | |
http://msdn.microsoft.com/en-us/library/ee663885(v=VS.85).aspx | |
#> | |
#Requires -Version 2.0 | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$true, | |
HelpMessage='Enter a valid download source: | |
Eg: | |
\\serverS\share\file.ext | |
or | |
http://serverS.com/file.ext | |
or | |
Cancel and re-run the script with the correct parameters | |
Full usage: | |
Start-TransferFiles.ps1 -s "path" -d "path" -c "credentials" | |
Full example: - Download single file from a file server | |
Start-TransferFile.ps1 -s "\\serverS\share\file.ext" -d "\\serverD\share" -c "domain\user" | |
Full example: - Download multiple files from a file server, using wildcard E.g. *.* or *.iso | |
Start-TransferFile.ps1 -s "\\serverS\share\*.*" -d "\\serverD\share"')] | |
[Alias("s")] | |
$SourcePath, | |
[Alias("d")] | |
$DestinationPath = $ENV:USERPROFILE, | |
[Alias("c")] | |
$Credentials = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name | |
)#End Param | |
#-------------------------- | |
function Start-TransferFile | |
{ | |
Try | |
{ | |
Import-Module bitstransfer | |
#Uncomment -Credential below if server requires alternate credentials | |
$Job = Start-BitsTransfer -DisplayName BitsJob -Source $SourcePath ` | |
-Destination $DestinationPath -Asynchronous # -Credential $Credentials -Authentication Passport | |
while (($Job.JobState -eq "Transferring") -or ($Job.JobState -eq "Connecting")) ` | |
{sleep 5; write-host $Job.JobState $Job.BytesTransferred 'bytes of' $Job.BytesTotal 'bytes'} | |
Switch($Job.JobState) | |
{ | |
"Transferred" {write-host "File successfully transferred to" $DestinationPath "at" $job.TransferCompletionTime; Complete-BitsTransfer -BitsJob $Job} | |
"Error" {$job.ErrorDescription;$job.ErrorCondition} # List the errors. | |
default {Remove-BitsTransfer -BitsJob $job} # Just kill the job if there is a problem and start again, this is not tested. | |
} | |
} | |
Catch [System.Management.Automation.RuntimeException] | |
{ | |
write-host ("Exception: " + $_.Exception.GetType().FullName) | |
write-host $_.Exception.Message | |
Remove-BitsTransfer -BitsJob $job | |
} | |
Finally | |
{ | |
Remove-Module Bits* | |
} | |
}#End Start-TransferFile | |
####################################################### | |
# Main | |
if ((get-service -name bits).status -eq "running") | |
{ | |
if ($SourcePath -like "http*") | |
{ | |
write-host "Source is Web Server" | |
Start-TransferFile | |
} | |
else | |
{ | |
write-host "Source is File Server" | |
if (!(test-path -path $SourcePath)) | |
{write-host "Source path" $SourcePath "does not exist"} | |
elseif (!(test-path -path $DestinationPath)) | |
{write-host "Destination path" $DestinationPath "does not exist"} | |
else | |
{Start-TransferFile} | |
} | |
} | |
else {write-host "BITS is not running, Please start the service First"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment