I am able to connect to a WebDAV site using WinSCP, and generated the code for that site in PowerShell. However, when I try the code to connect, I get an error saying the Basic challenge is rejected. I've been looking through code examples on the site and also through the forums before asking, but haven't found anything that allows me to handle this from a script. This script is a part of a scheduled automated workflow, so I would need a way to handle this that doesn't require any keyboard input.
Here's the barebones code generated by WinSCP:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Webdav
HostName = "example.com"
PortNumber = 443
UserName = "username"
Password = "password"
WebdavSecure = $True
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Your code
}
finally
{
$session.Dispose()
}
When connecting from the Windows app, I looked in the log & saw this:
. 2018-10-04 14:42:00.919 auth: Got challenge (code 401).
. 2018-10-04 14:42:00.919 auth: Got 'Basic' challenge.
. 2018-10-04 14:42:00.919 auth: Trying Basic challenge...
. 2018-10-04 14:42:00.919 auth: Accepted Basic challenge.
When I try my PowerShell script, I get this error:
. 2018-10-04 14:45:59.609 ah_post_send (#1), code is 401 (want 401), WWW-Authenticate is Basic realm="example.com"
. 2018-10-04 14:45:59.609 auth: Got challenge (code 401).
. 2018-10-04 14:45:59.609 auth: Got 'Basic' challenge.
. 2018-10-04 14:45:59.609 auth: Trying Basic challenge...
. 2018-10-04 14:45:59.609 auth: No challenges accepted.
. 2018-10-04 14:45:59.609 sess: Closing connection.
. 2018-10-04 14:45:59.609 sess: Connection closed.
. 2018-10-04 14:45:59.609 Request ends, status 401 class 4xx, error line:
. 2018-10-04 14:45:59.609 Could not authenticate to server: rejected Basic challenge
. 2018-10-04 14:45:59.609 Request ends.
. 2018-10-04 14:45:59.609 Asking user:
. 2018-10-04 14:45:59.609 Error listing directory '/Your Files'. ("Authentication failed.","Could not authenticate to server: rejected Basic challenge")
< 2018-10-04 14:45:59.609 Script: Error listing directory '/Your Files'.
< 2018-10-04 14:45:59.609 Script: Authentication failed.
< 2018-10-04 14:45:59.609 Could not authenticate to server: rejected Basic challenge
. 2018-10-04 14:45:59.609 Script: Failed
> 2018-10-04 14:45:59.796 Script: exit
. 2018-10-04 14:45:59.796 Script: Exit code: 1
. 2018-10-04 14:45:59.796 sess: Destroying session.
Any suggestions would be greatly appreciated. Thanks!