Map Network Drive to a WebDAV Server via PowerShell
My goal is to map a network drive in Windows to a WebDAV server via PowerShell.
I have a script that automatically creates an Azure VM with IIS installed and WebDAV configured. I can successfully map manually the network drive via Windows Explorer to the WebDAV server (using the option to use different credentials). That confirms that the WebDAV server is configured correctly. I can read and write files, too.
Via PowerShell I have tried to use the command New-PSDrive and get errors as you see.
New-PSDrive –Name $networkDrive –PSProvider FileSystem –Root "http://$serviceName.cloudapp.net/" –Persist
New-PSDrive : When you use the Persist parameter, the root must be a file system location on a remote computer.
At line:1 char:1
+ New-PSDrive –Name $networkDrive –PSProvider FileSystem –Root "http:// ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Z:PSDriveInfo) [New-PSDrive], NotSupportedException
+ FullyQualifiedErrorId : DriveRootNotNetworkPath,Microsoft.PowerShell.Commands.NewPSDriveCommand
OR without the parameter -Persist
New-PSDrive –Name $networkDrive –PSProvider FileSystem –Root "http://$serviceName.cloudapp.net/"
New-PSDrive : The specified drive root "http://webdavservertest3.cloudapp.net/" either does not exist, or it is not
a folder.
At line:1 char:1
+ New-PSDrive –Name $networkDrive –PSProvider FileSystem –Root "http:// ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (Z:PSDriveInfo) [New-PSDrive], IOException
+ FullyQualifiedErrorId : DriveRootError,Microsoft.PowerShell.Commands.NewPSDriveCommand
As one can see the term "http://$serviceName.cloudapp.net/"
successfully evaluates to "http://webdavservertest3.cloudapp.net/"
.
So, is this the right approach? Should New-PSDrive be capable of mapping to WebDAV servers? If not, any idea to map the network drive via PowerShell?
Solution 1:
Here is a working example of me mounting the Sysinternals WebDAV site to my S: drive:
[String]$WebDAVShare = '\\live.sysinternals.com\Tools'
New-PSDrive -Name S -PSProvider FileSystem -Root $WebDAVShare
Notice you need to use the UNC format, not the http://
prefix.
Also you need to make sure that the WebClient service is running on your computer.
If you wanted to confirm that a server supports WebDAV, you could do:
(Invoke-WebRequest http://live.sysinternals.com -Method Options).Headers.DAV
And if that returns something like 1,2,3
then the server supports various versions of WebDAV. (Although the server administrator may have disallowed the Options verb.)
Solution 2:
New-PSDrive -Name F -PSProvider FileSystem -Root "\\\\$FQDN_without_https@SSL/$URI_or_PathTo" -Credential <Username>
Please pay attention for @SSL
after FQDN.
This works for PowerShell 5.0.