使用New-PSDrive映射WebDAV后无法复制文件到远程服务器问题求解
问题根因
New-PSDrive默认创建的是会话级盘符,仅对当前PowerShell进程可见,Python等外部程序无法识别该盘符- Windows内置WebDAV客户端默认限制最大传输文件为50MB,超过会触发设备异常报错
- WebDAV映射路径的UNC格式解析优先级高于盘符,直接用UNC路径可以规避盘符识别问题
PowerShell解决方案
- 先调整WebDAV最大传输大小限制(需管理员权限执行)
# 修改注册表调整WebDAV最大文件传输限制为2GB,重启WebClient服务生效 Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\WebClient\Parameters" -Name "FileSizeLimitInBytes" -Value 2147483648 -Type DWord Restart-Service WebClient
- 拷贝文件优先选择以下两种方式规避盘符问题:
# 方式1:直接使用UNC路径拷贝,不需要映射盘符 [String]$WebDAVShare = '\\xxx.xxxxx.xxx@SSL\DavWWWRoot\webdav' Copy-Item -Path "ImportSheets\ToDo\xxxxx.xls" -Destination "$WebDAVShare\import\xxxxx.xls" -Credential $cred # 方式2:创建全局可见的盘符,添加-Persist参数后所有进程都可识别 New-PSDrive -Name "W" -PSProvider FileSystem -Root $WebDAVShare -Credential $cred -Persist Copy-Item -Path "ImportSheets\ToDo\xxxxx.xls" -Destination "W:\import\xxxxx.xls"
Python解决方案
推荐优先使用WebDAV专用库操作,不依赖系统映射配置,稳定性更高:
# 先安装依赖:pip install webdavclient3 from webdav3.client import Client options = { 'webdav_hostname': "https://xxx.xxxxx.xxx/webdav", 'webdav_login': "WebDAV登录账号", 'webdav_password': "WebDAV登录密码" } client = Client(options) # 直接上传本地文件到远程WebDAV路径 client.upload_sync(remote_path="import/xxxxx.xls", local_path="ImportSheets/ToDo/xxxxx.xls")
如果要复用系统映射的配置,也可直接用UNC路径访问:
import shutil webdav_unc = r'\\xxx.xxxxx.xxx@SSL\DavWWWRoot\webdav' shutil.copy(r'ImportSheets\ToDo\xxxxx.xls', fr'{webdav_unc}\import\xxxxx.xls')
内容的提问来源于stack exchange,提问作者Stuif
相关产品推荐
相关产品推荐

