|
| 1 | +<# |
| 2 | + OpenCoperLock — mount your Drive as a Windows network drive over WebDAV. |
| 3 | +
|
| 4 | + Windows' built-in WebDAV client (the "WebClient" service) needs a few registry tweaks before it |
| 5 | + behaves, and it mislabels the mount. This script does the whole thing in one shot: |
| 6 | +
|
| 7 | + 1. Configures the WebClient service (Basic-over-HTTPS, raises the 50 MB download cap, longer |
| 8 | + timeout) — this part self-elevates to Administrator. |
| 9 | + 2. Restarts WebClient so the settings take effect and its "not a WebDAV server" cache is cleared. |
| 10 | + 3. Maps your Drive to a drive letter (default X:) that reconnects at logon. |
| 11 | + 4. Gives the drive a proper label instead of Windows' ugly default ("dav"). |
| 12 | +
|
| 13 | + USAGE (right-click the .cmd wrapper → it calls this, or run directly): |
| 14 | +
|
| 15 | + powershell -ExecutionPolicy Bypass -File .\mount-opencoperlock-windows.ps1 ` |
| 16 | + -Server copper.forgenet.fr -Drive X -Token ocl_XXXXXXXX -Label "OpenCoperLock" |
| 17 | +
|
| 18 | + The token is your PERSONAL API token (Account → API tokens) — an unrestricted one; folder-scoped |
| 19 | + tokens are refused by WebDAV. If you omit -Token the script prompts for it. |
| 20 | +
|
| 21 | + NOTE ON "free space": Windows often shows your LOCAL C: drive's size on a WebDAV mount instead of |
| 22 | + your account quota. That's a Windows limitation — the server does report the correct quota. Your |
| 23 | + real usage is always shown in the web app. |
| 24 | +#> |
| 25 | +[CmdletBinding()] |
| 26 | +param( |
| 27 | + [string]$Server = "copper.forgenet.fr", |
| 28 | + [string]$BasePath = "/api/dav", |
| 29 | + [string]$Drive = "X", |
| 30 | + [string]$Token, |
| 31 | + [string]$User = "me", |
| 32 | + [string]$Label = "OpenCoperLock", |
| 33 | + # Internal: set when the script relaunches itself elevated to do only the registry/service part. |
| 34 | + [switch]$ConfigureOnly |
| 35 | +) |
| 36 | + |
| 37 | +$ErrorActionPreference = "Stop" |
| 38 | +$Drive = $Drive.TrimEnd(":") |
| 39 | + |
| 40 | +function Test-Admin { |
| 41 | + $id = [Security.Principal.WindowsIdentity]::GetCurrent() |
| 42 | + (New-Object Security.Principal.WindowsPrincipal($id)).IsInRole( |
| 43 | + [Security.Principal.WindowsBuiltinRole]::Administrator) |
| 44 | +} |
| 45 | + |
| 46 | +# ── Phase A: the Administrator-only part (registry + service) ───────────────────────────────── |
| 47 | +function Set-WebClientConfig { |
| 48 | + $p = "HKLM:\SYSTEM\CurrentControlSet\Services\WebClient\Parameters" |
| 49 | + Set-ItemProperty $p -Name BasicAuthLevel -Value 2 -Type DWord |
| 50 | + Set-ItemProperty $p -Name FileSizeLimitInBytes -Value 0xFFFFFFFF -Type DWord # ~4 GB (was 50 MB) |
| 51 | + Set-ItemProperty $p -Name FsCtlRequestTimeoutInSec -Value 300 -Type DWord |
| 52 | + Set-Service WebClient -StartupType Automatic |
| 53 | + Restart-Service WebClient -Force |
| 54 | + Write-Host "[ok] WebClient configured and restarted." -ForegroundColor Green |
| 55 | +} |
| 56 | + |
| 57 | +if ($ConfigureOnly) { |
| 58 | + # We are the elevated child: do only the privileged part, then exit. |
| 59 | + Set-WebClientConfig |
| 60 | + return |
| 61 | +} |
| 62 | + |
| 63 | +# ── Phase B: the normal-user part (mapping must NOT run elevated, or Explorer won't see it) ───── |
| 64 | +if (-not $Token) { $Token = Read-Host "Paste your OpenCoperLock API token (ocl_...)" } |
| 65 | +if (-not $Token) { throw "No token provided." } |
| 66 | + |
| 67 | +Write-Host "Configuring the WebClient service (a UAC prompt will appear)..." -ForegroundColor Cyan |
| 68 | +$childArgs = @( |
| 69 | + "-ExecutionPolicy","Bypass","-File","`"$PSCommandPath`"","-ConfigureOnly", |
| 70 | + "-Server",$Server,"-BasePath",$BasePath |
| 71 | +) |
| 72 | +$proc = Start-Process -FilePath "powershell.exe" -Verb RunAs -ArgumentList $childArgs -Wait -PassThru |
| 73 | +if ($proc.ExitCode -ne 0) { Write-Warning "The elevated configuration step reported an error; continuing to the mount anyway." } |
| 74 | + |
| 75 | +# The @SSL UNC form routes reliably through the WebDAV redirector over HTTPS on port 443. |
| 76 | +$uncPath = "\\$Server@SSL" + ($BasePath -replace "/","\") |
| 77 | + |
| 78 | +# Drop any stale mapping on that letter first (ignore "not connected" errors). |
| 79 | +& net.exe use "$($Drive):" /delete /y 2>$null | Out-Null |
| 80 | + |
| 81 | +Write-Host "Mapping $($Drive): -> $uncPath ..." -ForegroundColor Cyan |
| 82 | +& net.exe use "$($Drive):" $uncPath /user:$User $Token /persistent:yes |
| 83 | +if ($LASTEXITCODE -ne 0) { |
| 84 | + throw "net use failed (exit $LASTEXITCODE). Make sure the WebClient service is running and the token is valid." |
| 85 | +} |
| 86 | + |
| 87 | +# Give the drive a human label instead of Windows' default ("dav") via MountPoints2. |
| 88 | +try { |
| 89 | + $mangled = "##" + (($uncPath.TrimStart("\")) -replace "[\\/]","#") |
| 90 | + $mp = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\$mangled" |
| 91 | + New-Item -Path $mp -Force | Out-Null |
| 92 | + Set-ItemProperty -Path $mp -Name "_LabelFromReg" -Value $Label |
| 93 | + Write-Host "[ok] Drive labelled '$Label'." -ForegroundColor Green |
| 94 | +} catch { |
| 95 | + Write-Warning "Could not set the drive label (cosmetic only): $($_.Exception.Message)" |
| 96 | +} |
| 97 | + |
| 98 | +Write-Host "" |
| 99 | +Write-Host "Done. Your Drive is mounted at $($Drive): — open 'This PC'." -ForegroundColor Green |
| 100 | +Write-Host "(Windows may show your local disk size for 'free space'; that's a Windows quirk, not your real quota.)" -ForegroundColor DarkGray |
0 commit comments