0

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

任何人的想法,这怎么可能做到?

更新:我发现可以使用 PROPFIND。我得到一个包含所有目录数据的 XML。

4

2 回答 2

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'
于 2020-07-21T10:17:33.217 回答
0

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

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

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

我敢肯定,一定有比这更容易/更好的方法。

于 2012-11-15T19:53:24.587 回答