Advertisement
AlphaOmega2020

Download Script

Jul 23rd, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. # URL of the XML file containing the list of source URLs
  2. $sourceXmlUrl = "https://raw.githubusercontent.com/test/xmls/test.xml"
  3.  
  4. # Registry path to the key containing the destination folder
  5. $registryPath = "HKCU:\Environment"
  6. $registryValueName = "OneDrive"
  7.  
  8. # Segments to ignore from the URL when constructing the destination folder
  9. $ignoreSegments = @("https://raw.githubusercontent.com/test/")
  10.  
  11. function Get-SanitizedPath {
  12. param (
  13. [string]$path
  14. )
  15.  
  16. $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
  17. $sanitizedPath = $path -replace "[$invalidChars]", '-'
  18. return $sanitizedPath
  19. }
  20.  
  21. try {
  22. # Read the destination folder path from the registry
  23. $destinationFolder = Get-ItemProperty -Path $registryPath -Name $registryValueName | Select-Object -ExpandProperty $registryValueName
  24.  
  25. # Create the destination folder if it doesn't exist
  26. if (-Not (Test-Path -Path $destinationFolder -PathType Container)) {
  27. New-Item -ItemType Directory -Path $destinationFolder -Force | Out-Null
  28. }
  29.  
  30. # Download the XML file
  31. $webClient = New-Object System.Net.WebClient
  32. $xmlData = $webClient.DownloadString($sourceXmlUrl)
  33.  
  34. # Load the XML data
  35. $xmlDoc = [System.Xml.XmlDocument]::new()
  36. $xmlDoc.LoadXml($xmlData)
  37.  
  38. # Extract the URLs from the XML
  39. $sourceUrls = $xmlDoc.SelectNodes("//urls/url")
  40.  
  41. # Download each file from the URLs and save to the destination folder
  42. foreach ($urlNode in $sourceUrls) {
  43. $url = $urlNode.InnerText
  44.  
  45. # Remove the ignored segments from the URL
  46. foreach ($segment in $ignoreSegments) {
  47. $url = $url -replace [regex]::Escape($segment), ""
  48. }
  49.  
  50. # Split the URL by directory separator ("/") to create the folder structure
  51. $urlSegments = $url -split "/"
  52. $urlSegments = $urlSegments | Where-Object { $_ -ne "" }
  53.  
  54. # Sanitize each URL segment to create valid folder names
  55. $sanitizedSegments = $urlSegments | ForEach-Object { Get-SanitizedPath -path $_ }
  56.  
  57. # Combine the destination folder and the sanitized URL segments to form the complete destination path
  58. $destinationPath = Join-Path $destinationFolder $sanitizedSegments
  59.  
  60. # Convert the path to use the correct directory separator for the current platform
  61. $destinationPath = $destinationPath -replace '\\', [IO.Path]::DirectorySeparatorChar
  62.  
  63. # Download the file and save it to the destination folder
  64. try {
  65. # If the file is in the root, the destination path will be the root itself
  66. if ($sanitizedSegments.Count -eq 0) {
  67. $destinationPath = $destinationFolder
  68. }
  69.  
  70. $webClient.DownloadFile($url, $destinationPath)
  71. Write-Host "Downloaded file: $($urlSegments[-1])"
  72. } catch {
  73. Write-Host "Error downloading file: $_.Exception.Message"
  74. }
  75. }
  76. Write-Host "All files downloaded successfully!"
  77. } catch {
  78. Write-Host "Error: $_.Exception.Message"
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement