29 lines
1.5 KiB
PowerShell
29 lines
1.5 KiB
PowerShell
$REMOTE='https://webdav.example.com/';
|
|
$REMOTEFOLDER = 'folder';
|
|
$USERNAME = '';
|
|
$PASSWORD = '';
|
|
|
|
# Define custom auth header
|
|
$authH = @{Authorization='Basic {0}' -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("${USERNAME}:${PASSWORD}")))};
|
|
|
|
# Define custom WebRequest function to allow WebDav-Methods PROPFIND and MKCOL
|
|
# With PowerShell 6 use "Invoke-RestMethod -CustomMethod" instead
|
|
function Invoke-CustomRequest($Method, $Url){
|
|
$req = [net.webrequest]::create($Url);
|
|
$req.Headers['Authorization'] = $authH.Authorization;
|
|
$req.Headers['Depth'] = '1';
|
|
$req.Method = $Method;
|
|
$reader = [System.IO.StreamReader]::new($req.GetResponse().GetResponseStream());
|
|
$props = $reader.ReadToEnd();
|
|
return $props;
|
|
}
|
|
|
|
# Clean up all files on remote: List all files on root (PROPFIND), XPath to find hrefs then call DELETE for each
|
|
Invoke-CustomRequest -Method 'PROPFIND' -Url $REMOTE/$REMOTEFOLDER/ | Select-Xml -XPath "//*[local-name()='href']" | % { $_.Node.'#text' } | % { Invoke-CustomRequest -Method "DELETE" -Url "$REMOTE/$_" }
|
|
|
|
# Create all directories: find all directories with relative paths and call MKCOL to create on remote
|
|
Get-ChildItem -Directory -Recurse | % {$_.FullName | Resolve-Path -Relative} | % { Invoke-CustomRequest -Method "MKCOL" -Url "{0}/{1}" -f $REMOTE,$_ -Replace '\\','/' }
|
|
|
|
# Copy all files: find all files with relative paths then call PUT
|
|
Get-ChildItem -File -Recurse | % {$_.FullName | Resolve-Path -Relative} | % { Invoke-WebRequest -Method "PUT" -Headers $authH -Infile $_ "{0}/{1}" -f $REMOTE,$_ -Replace '\\','/' }
|