Advertisement
Guest User

Untitled

a guest
Mar 18th, 2022
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. ##Refrence material:
  2. ##https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-7.2
  3. ##https://lazyadmin.nl/powershell/download-file-powershell/
  4. ##https://shellgeek.com/powershell-create-directory-if-not-exists/
  5. ##https://4sysops.com/archives/use-powershell-to-download-a-file-with-http-https-and-ftp/
  6.  
  7. #Set destination to download file too.
  8. $destination = "c:\temp\downloads"
  9.  
  10. #Ask tech where to download from.
  11. $source = "ServerUtilsAskTech (URL,text,http://test)"
  12.  
  13. #Check if destination exists. If not create it.
  14. if (Test-Path $destination) {
  15. Write-Host "Folder Exists"
  16. }
  17. else
  18. {
  19. #PowerShell Create directory if not exists
  20. New-Item $destination -ItemType Directory
  21. Write-Host "Folder Created successfully"
  22. }
  23.  
  24. #get file name from download url
  25. $destination = $destination + $(Split-Path -Path $source -Leaf)
  26.  
  27. #Invoke-WebRequest -Uri $source -OutFile $destination
  28.  
  29. Import-Module BitsTransfer
  30. $Job = Start-BitsTransfer -Source $source -Destination $destination -Asynchronous -Priority normal -TransferType Download
  31. echo "Running Job"
  32.  
  33. while (($Job.JobState -eq "Transferring") -or ($Job.JobState -eq "Connecting")) `
  34. { sleep 5;} # Poll for status, sleep for 5 seconds, or perform an action.
  35.  
  36. echo "completing job"
  37. echo $Job
  38.  
  39. Switch($Job.JobState)
  40. {
  41. "Transferred" {Complete-BitsTransfer -BitsJob $Job}
  42. "Error" {$Job | Format-List } # List the errors.
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement