Loading
May/10
20
PowerShell : Upload file to WebDav Server
1 Comment · Posted by Christopher Keyaert in PowerShell
The purpose of that Powershell Script is to upload a file on a webdav server. This could be useful for automatic report publishing on a portal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | ######################################## #Webdav Access with PowerShell ######################################## #Put the complete path of the file that you want to upload $file = "D:\test.txt" #Put the url without the last "/" $url = "http://mywebSite/webdav" #Provide User and Pwd for Webdav Access $user = "user" $pass = "pwd" ######################################## #Script ####################################### #Adding the name of the file at the end of the URL $url += "/" + $file.split('\')[(($file.split("\")).count - 1)] #Connecting to WebDav Write-Host "File upload started" # Set binary file type Set-Variable -name adFileTypeBinary -value 1 -option Constant $objADOStream = New-Object -ComObject ADODB.Stream $objADOStream.Open() $objADOStream.Type = $adFileTypeBinary $objADOStream.LoadFromFile("$file") $arrbuffer = $objADOStream.Read() $objXMLHTTP = New-Object -ComObject MSXML2.ServerXMLHTTP $objXMLHTTP.Open("PUT", $url, $False, $user, $pass) $objXMLHTTP.send($arrbuffer) Write-Host "File upload finished" |
And that’s it
Christopher Keyaert
PowerShell · script · upload · webdav

sensimilla · March 31, 2012 at 5:53 AM
You can make this script accept a self signed certificate for https: Useful if you just want to access your home NAS.
Change the line:
$objXMLHTTP = New-Object -ComObject MSXML2.ServerXMLHTTP
To:
$objXMLHTTP = New-Object -ComObject MSXML2.ServerXMLHTTP.6.0
and add this line below it:
$objXMLHTTP.setOption(2,13056)
This makes the script ignore any certificate errors.