tacticalrmm/scripts/Win_File_Copy_Misc.ps1

42 lines
1.7 KiB
PowerShell

# Requires WebClient object $webClient defined, e.g. $webClient = New-Object System.Net.WebClient
#
# Parameters:
# $source - The url of folder to copy, with trailing /, e.g. http://website/folder/structure/
# $destination - The folder to copy $source to, with trailing \ e.g. D:\CopyOfStructure\
# $recursive - True if subfolders of $source are also to be copied or False to ignore subfolders
Function Copy-Folder([string]$source, [string]$destination, [bool]$recursive) {
if (!$(Test-Path($destination))) {
New-Item $destination -type directory -Force
}
# Get the file list from the web page
$webString = $webClient.DownloadString($source)
$lines = [Regex]::Split($webString, "<br>")
# Parse each line, looking for files and folders
foreach ($line in $lines) {
if ($line.ToUpper().Contains("HREF")) {
# File or Folder
if (!$line.ToUpper().Contains("[TO PARENT DIRECTORY]")) {
# Not Parent Folder entry
$items = [Regex]::Split($line, """")
$items = [Regex]::Split($items[2], "(>|<)")
$item = $items[2]
if ($line.ToLower().Contains("&lt;dir&gt")) {
# Folder
if ($recursive) {
# Subfolder copy required
Copy-Folder "$source$item/" "$destination$item/" $recursive
}
else {
# Subfolder copy not required
}
}
else {
# File
$webClient.DownloadFile("$source$item", "$destination$item")
}
}
}
}
}