From e9e5bf31a7fa35c4d6f323c19585f4c783648793 Mon Sep 17 00:00:00 2001 From: silversword411 Date: Tue, 27 Apr 2021 12:50:01 -0400 Subject: [PATCH] script library adding file copy script --- .../scripts/community_scripts.json | 10 +++++ scripts/Win_File_Copy_Misc.ps1 | 42 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 scripts/Win_File_Copy_Misc.ps1 diff --git a/api/tacticalrmm/scripts/community_scripts.json b/api/tacticalrmm/scripts/community_scripts.json index 29f8d7f8..65997501 100644 --- a/api/tacticalrmm/scripts/community_scripts.json +++ b/api/tacticalrmm/scripts/community_scripts.json @@ -599,5 +599,15 @@ "description": "Add a task to Task Scheduler, needs editing", "shell": "powershell", "category": "TRMM (Win):Other" + }, + { + "guid": "17040742-184a-4251-8f7b-4a1b0a1f02d1", + "filename": "Win_File_Copy_Misc.ps1", + "submittedBy": "https://github.com/tremor021", + "name": "EXAMPLE File Copying using powershell", + "description": "Reference Script: Will need manual tweaking, for copying files/folders from paths/websites to local", + "shell": "powershell", + "category": "TRMM (Win):Misc", + "default_timeout": "1" } ] \ No newline at end of file diff --git a/scripts/Win_File_Copy_Misc.ps1 b/scripts/Win_File_Copy_Misc.ps1 new file mode 100644 index 00000000..261323a4 --- /dev/null +++ b/scripts/Win_File_Copy_Misc.ps1 @@ -0,0 +1,42 @@ +# 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, "
") + # 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("<dir>")) { + # 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") + } + } + } + } +} \ No newline at end of file