Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # URL of the XML file containing the list of source URLs
- $sourceXmlUrl = "https://raw.githubusercontent.com/test/xmls/test.xml"
- # Registry path to the key containing the destination folder
- $registryPath = "HKCU:\Environment"
- $registryValueName = "OneDrive"
- # Segments to ignore from the URL when constructing the destination folder
- $ignoreSegments = @("https://raw.githubusercontent.com/test/")
- function Get-SanitizedPath {
- param (
- [string]$path
- )
- $invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
- $sanitizedPath = $path -replace "[$invalidChars]", '-'
- return $sanitizedPath
- }
- try {
- # Read the destination folder path from the registry
- $destinationFolder = Get-ItemProperty -Path $registryPath -Name $registryValueName | Select-Object -ExpandProperty $registryValueName
- # Create the destination folder if it doesn't exist
- if (-Not (Test-Path -Path $destinationFolder -PathType Container)) {
- New-Item -ItemType Directory -Path $destinationFolder -Force | Out-Null
- }
- # Download the XML file
- $webClient = New-Object System.Net.WebClient
- $xmlData = $webClient.DownloadString($sourceXmlUrl)
- # Load the XML data
- $xmlDoc = [System.Xml.XmlDocument]::new()
- $xmlDoc.LoadXml($xmlData)
- # Extract the URLs from the XML
- $sourceUrls = $xmlDoc.SelectNodes("//urls/url")
- # Download each file from the URLs and save to the destination folder
- foreach ($urlNode in $sourceUrls) {
- $url = $urlNode.InnerText
- # Remove the ignored segments from the URL
- foreach ($segment in $ignoreSegments) {
- $url = $url -replace [regex]::Escape($segment), ""
- }
- # Split the URL by directory separator ("/") to create the folder structure
- $urlSegments = $url -split "/"
- $urlSegments = $urlSegments | Where-Object { $_ -ne "" }
- # Sanitize each URL segment to create valid folder names
- $sanitizedSegments = $urlSegments | ForEach-Object { Get-SanitizedPath -path $_ }
- # Combine the destination folder and the sanitized URL segments to form the complete destination path
- $destinationPath = Join-Path $destinationFolder $sanitizedSegments
- # Convert the path to use the correct directory separator for the current platform
- $destinationPath = $destinationPath -replace '\\', [IO.Path]::DirectorySeparatorChar
- # Download the file and save it to the destination folder
- try {
- # If the file is in the root, the destination path will be the root itself
- if ($sanitizedSegments.Count -eq 0) {
- $destinationPath = $destinationFolder
- }
- $webClient.DownloadFile($url, $destinationPath)
- Write-Host "Downloaded file: $($urlSegments[-1])"
- } catch {
- Write-Host "Error downloading file: $_.Exception.Message"
- }
- }
- Write-Host "All files downloaded successfully!"
- } catch {
- Write-Host "Error: $_.Exception.Message"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement