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

使用PowerShell通过WebDAV下载特定目录中的所有文件,powershell,download,webdav,directory-listing,Powershell,Download,Webdav,Directory Listing,我必须获得一个文件列表,并通过WebDAV下载PowerShell列出的所有这些文件。下载部分不是大问题,但我无法使用PowerShell获得目录列表。 据我所见,谷歌在这个话题上没有找到一个有用的想法。我设法从一个目录上载了所有文件,但get childitems在WebDAV上的远程目录上不起作用 有人知道怎么做吗 更新: 我想,PROPFIND是可以使用的。我正在获取一个包含所有目录数据的XML。如果启用了目录浏览,您应该能够获得包含以下内容的列表: $wc = new-object sy

我必须获得一个文件列表,并通过WebDAV下载PowerShell列出的所有这些文件。下载部分不是大问题,但我无法使用PowerShell获得目录列表。 据我所见,谷歌在这个话题上没有找到一个有用的想法。我设法从一个目录上载了所有文件,但get childitems在WebDAV上的远程目录上不起作用

有人知道怎么做吗

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

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

$wc = new-object system.net.webclient;
$dirList = $wc.downloadstring(URL_TO_DIRECTORY);
从这里开始,您必须解析HTML以查找文件名,然后使用
$wc.downloadfile()
将每个文件都下拉


我相信一定有比这更简单/更好的方法。

我也有类似的问题,在搜索更多信息时发现了这个问题。最终,我使用以下代码通过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'

不幸的是,这给了我一个404。但是谢谢你的回答。嗨,Bwvanmanen,欢迎来到SO,谢谢你的回答。如果您可以包含找到解决方案的链接,则可能会对人们有所帮助。