Dane
Dane

Reputation: 13

Download all files in a specific directory over WebDAV with PowerShell

I have to get a file listing and download all these files listed with PowerShell over WebDAV. The part with downloading isn't the big problem, but I'm not able to get a directory listing with PowerShell. Google didn't find a useful idea on that topic, from what I've seen. I managed to upload all files from a directory, but get-childitems doesn't work on remote directories over WebDAV.

Anybody an idea, how this could be done?

Update: I figured out, that the PROPFIND can be used. I am getting an XML with all the directory data.

Upvotes: 1

Views: 4422

Answers (2)

bwvanmanen
bwvanmanen

Reputation: 11

I had a similar problem and found this issue when searching for more information. Eventually I got the PowerShell file transfer via WebDAV to work using the following code:

# Script to download files using a WebDAV file transfer connection. 
# Original script by Andrew Pla: https://andrewpla.dev/Download-TOPdesk-Backups-Using-PowerShell/
# Modified for downloading multiple files from a WebDAV folder in a TOPdesk environment. 

 
### Variables ###
# Where can we download the files to? 
$OutputFolder = 'D:\temp'

# What is your environment URL? 
$TOPdeskURL = 'pentest.topdesk.net' #Example: 'customername.topdesk.net'

#What folder are we downloading from? 
$UploadFolder = 'upload/incident'       #Use a forward slash to indicate subfolders. Example upload/incident

#What is the pattern we can use to identify folders to download? 
$folderPattern = "^I"   #Uses regular expressions to match a list of folders. Example: ^I16 matches all folders that start with I16. 
                            #Full documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-7


### The actual script starts here. You don't have to edit anything below this line ###
#
# Ask credentials for an operator with webdav access
$Credential = Get-Credential -Message 'Enter credentials for a TOPdesk operator with WebDAV file transfer permissions.'

# Map remote folder to a temporary drive called TOPdesk
$psDriveParams = @{
    PSProvider = 'FileSystem'
    Root = "\\$TOPdeskURL@SSL\webdav" 
    Credential = $Credential
    Name = 'TOPdesk'
}
New-PSDrive @psDriveParams


# Create a webclient that we will use to download the file.
$WebClient = New-Object system.net.webclient

# Add TOPdesk credentials for the connection, proving TOPdesk may send us the file.
$WebClient.Credentials = $Credential

#Download all folders that match the pattern
$folders = Get-ChildItem TOPdesk:\$UploadFolder |Where-Object{$_.name -Match $folderPattern} #|select -exp fullname
    if ($folders) {
    write-host 'Downloading the following folders: '

        Foreach($folder in $folders){
            # Download the file by using the downloadfile method on the web client. 
            # Copy files from the mapped drive to the download folder from the variables list
            write-host $folder
            Copy-Item "TOPdesk:\$UploadFolder\$folder" -Destination "$OutputFolder\$folder" -Recurse
        }
    }
    elseif (-not $folders){
        # clean up the temporary drive mapping that we created.
        Remove-PSDrive 'TOPdesk'
        write-host 'No folders found. Run Get-ChildItem Topdesk:\' + $UploadFolder + ' to check if files exist, or explore the remote folder in a tool like WinSCP.'
    }
    else {
        Remove-PSDrive 'TOPdesk'
        throw 'There was a problem determining what folders to copy. Please review your variables.'
    }

# clean up the temporary drive mapping that we created.
Remove-PSDrive 'TOPdesk'

Upvotes: 1

alroc
alroc

Reputation: 28174

If directory browsing is enabled, you should be able to get a listing with this:

$wc = new-object system.net.webclient;
$dirList = $wc.downloadstring(URL_TO_DIRECTORY);

From there, you'll have to parse out the HTML to find the file names, and use $wc.downloadloadfile() to pull each one down.

I'm sure there must be an easier/better way than this.

Upvotes: 0

Related Questions