使用PowerShell通过WebDAV下载特定目录中的所有文件

时间:2012-11-15 16:34:00

标签: powershell download webdav directory-listing

我必须获取一个文件列表,并通过WebDAV下载PowerShell列出的所有这些文件。下载部分不是大问题,但我无法使用PowerShell获取目录列表。 从我所看到的,谷歌没有找到关于这个主题的有用的想法。我设法从目录上传所有文件,但get-childitems不能通过WebDAV在远程目录上运行。

任何人都有想法,如何做到这一点?

更新: 我想通了,可以使用PROPFIND。我正在获取包含所有目录数据的XML。

2 个答案:

答案 0 :(得分:1)

我遇到了类似的问题,并且在搜索更多信息时发现了此问题。最终,我通过WebDAV获得了PowerShell文件传输,可以使用以下代码进行工作:

# 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://docs.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'

答案 1 :(得分:0)

如果启用了目录浏览,您应该能够获得以下列表:

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

从那里,您必须解析HTML以查找文件名,并使用$wc.downloadloadfile()将每个文件名拉下来。

我确信必须有一种比这更简单/更好的方式。