SimpleHelp Community

Script for downloading files


#1

I keep running into areas with crappy internet and I need to prep a client computer without disturbing them. So I made a script to download a file from a URL. Example: The client is busy. I cant take over the machine. I can’t download the file to my computer and then transfer it because of crappy internet. I need to download the large file directly to the client’s machine from a service provider, normally done via a browser.

The script asks the tech for the URL of the file then downloads it to the client machine under c:\temp\downloads.

Hopefully, this will help someone else as well. Maybe you can even improve it with a download bar or time remaining etc…hint hint. :slight_smile:

##Refrence material:
##https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-7.2
##https://lazyadmin.nl/powershell/download-file-powershell/
##https://shellgeek.com/powershell-create-directory-if-not-exists/
##https://4sysops.com/archives/use-powershell-to-download-a-file-with-http-https-and-ftp/

#Set destination to download file too.
$destination = “c:\temp\downloads”

#Ask tech where to download from.
$source = “ServerUtilsAskTech (URL,text,http://test)”

#Check if destination exists. If not create it.
if (Test-Path $destination) {
Write-Host “Folder Exists”
}
else
{
#PowerShell Create directory if not exists
New-Item $destination -ItemType Directory
Write-Host “Folder Created successfully”
}

#get file name from download url
$destination = $destination + $(Split-Path -Path $source -Leaf)

#Invoke-WebRequest -Uri $source -OutFile $destination

Import-Module BitsTransfer
$Job = Start-BitsTransfer -Source $source -Destination $destination -Asynchronous -Priority normal -TransferType Download
echo “Running Job”

while (($Job.JobState -eq “Transferring”) -or ($Job.JobState -eq “Connecting”)) `
{ sleep 5;} # Poll for status, sleep for 5 seconds, or perform an action.

echo “completing job”
echo $Job

Switch($Job.JobState)
{
“Transferred” {Complete-BitsTransfer -BitsJob $Job}
“Error” {$Job | Format-List } # List the errors.
}


#2

Seems like this would be very convenient indeed. I tried it and got the following error:

At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:26

  • while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
  •                      ~
    

You must provide a value expression following the ‘-eq’ operator.
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:27

  • while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
  •                       ~~~~~~~~~~~~~~~~~~
    

Unexpected token ‘�?oTransferring�??’ in expression or statement.
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:26

  • while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
  •                      ~
    

Missing closing ‘)’ in expression.
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:26

  • while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
  •                      ~
    

Missing closing ‘)’ after expression in ‘while’ statement.
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:45

  • while (($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq …
  •                                         ~
    

Unexpected token ‘)’ in expression or statement.
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:69

  • … ($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq �?oConn …
  •                                                              ~
    

You must provide a value expression following the ‘-eq’ operator.
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:70

  • … q �?oTransferring�??) -or ($Job.JobState -eq �?oConnecting�??)) { sle …
  •                                              ~~~~~~~~~~~~~~~~
    

Unexpected token ‘�?oConnecting�??’ in expression or statement.
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:69

  • … ($Job.JobState -eq �?oTransferring�??) -or ($Job.JobState -eq �?oConn …
  •                                                              ~
    

Missing closing ‘)’ in expression.
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:86

  • … �?oTransferring�??) -or ($Job.JobState -eq �?oConnecting�??)) { slee …
  •                                                             ~
    

Unexpected token ‘)’ in expression or statement.
At C:\WINDOWS\TEMP\toolbox8036547725440715274run\Download20File-2-1647629459317-eudpw7gh.ps1:33 char:87

  • … �?oTransferring�??) -or ($Job.JobState -eq �?oConnecting�??)) { sleep …
  •                                                             ~
    

Unexpected token ‘)’ in expression or statement.
+ CategoryInfo : ParserError: (:slight_smile: [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression


#3

I think it’s just “smart curly quotes” instead of “boring straight quotes” issue - does this boring version work?


#4

Yes, this works! I had to add a trailing slash after c:\temp\downloads but its working - this will be very handy. I was always connecting command line and using curl - but nw I will use this. Thank you for sharing!


#5

ah,ya. Something probably got formated wierd when I copy and pasted it from my toolbox.
Glad you got it worked out.