Author Posts

February 11, 2016 at 12:08 am

Helpful friends,

I am trying to use powershell to upload files to a WebDAV server. I am able to upload using a simple username and password using various apps on my iPhone/iPad. However, I continually receive the error "The remote server returned an error: (401) Unauthorized." This seems like a credential error, but nothing seems to work. I am using powershell 2.0. Code below:


$ServerURL = "http://website.com/location"
$Username = "Name"
$Password = "Pass"

function Publish-File {
param (
[parameter( Mandatory = $true, HelpMessage="URL pointing to a SharePoint document library (omit the '/forms/default.aspx' portion)." )]
[System.Uri]$Url,
#[system.Management.Automation.PSCredential]$Credential,
[parameter( Mandatory = $true, ValueFromPipeline = $true, HelpMessage="One or more files to publish. Use 'dir' to produce correct object type." )]
[System.IO.FileInfo[]]$FileName
)
Add-Type -AssemblyName System.Web

$wc = new-object System.Net.WebClient
$wc.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)

$FileName | ForEach-Object {

$DestUrl = [system.Web.HttpUtility]::UrlPathEncode( ( "{0}{1}{2}" -f $Url.ToString().TrimEnd("/"), "/", $_.Name ) )
Write-Host "$( get-date -f s ): Uploading file: $_"
Write-Host "url: $DestUrl"
$wc.UploadFile( $DestUrl , "PUT", $_.FullName )
Write-Host "$( get-date -f s ): Upload completed"

}

}

##upload document
dir "c:\temp2" | Publish-File -Url $ServerURL

Thanks

February 11, 2016 at 8:23 am

WebDAV usually has its own authentication apart from the HTTP socket, and that's likely what you're running into. I'm not familiar enough with your setup to offer a suggestion though.