Files
win-remote-toolkit/scripts/powershell/map-anonymous-drive.ps1
2026-04-24 17:44:02 +08:00

145 lines
4.5 KiB
PowerShell

[Console]::OutputEncoding = [Text.Encoding]::UTF8
$ErrorActionPreference = 'Continue'
Write-Host "[*] Mapping R: drive with anonymous access (no credentials)"
$networkPath = "\\192.168.88.231\RD"
$driveLetter = "R:"
# First, remove any existing R: mapping
Write-Host "[*] Cleaning up existing mappings"
try {
& net use $driveLetter /delete /yes 2>$null
Write-Host " Cleared any existing R: mapping"
} catch {
Write-Host " No existing mapping to clear"
}
Write-Host "[*] Testing anonymous access methods"
# Method 1: Simple net use without credentials
Write-Host "[1] Trying simple net use..."
try {
$result = & net use $driveLetter $networkPath /persistent:yes
if ($LASTEXITCODE -eq 0) {
Write-Host "[OK] Method 1 successful!"
$success = $true
} else {
Write-Host " Method 1 failed: $result"
$success = $false
}
} catch {
Write-Host " Method 1 exception: $($_.Exception.Message)"
$success = $false
}
# Method 2: Explicit guest access
if (-not $success) {
Write-Host "[2] Trying guest access..."
try {
$result = & net use $driveLetter $networkPath /user:guest "" /persistent:yes
if ($LASTEXITCODE -eq 0) {
Write-Host "[OK] Method 2 successful!"
$success = $true
} else {
Write-Host " Method 2 failed: $result"
}
} catch {
Write-Host " Method 2 exception: $($_.Exception.Message)"
}
}
# Method 3: Empty username
if (-not $success) {
Write-Host "[3] Trying empty username..."
try {
$result = & net use $driveLetter $networkPath /user:"" "" /persistent:yes
if ($LASTEXITCODE -eq 0) {
Write-Host "[OK] Method 3 successful!"
$success = $true
} else {
Write-Host " Method 3 failed: $result"
}
} catch {
Write-Host " Method 3 exception: $($_.Exception.Message)"
}
}
# Method 4: Using PowerShell New-PSDrive
if (-not $success) {
Write-Host "[4] Trying PowerShell PSDrive..."
try {
New-PSDrive -Name "R" -PSProvider FileSystem -Root $networkPath -Persist -ErrorAction Stop
Write-Host "[OK] Method 4 successful!"
$success = $true
} catch {
Write-Host " Method 4 exception: $($_.Exception.Message)"
}
}
# Verify the mapping
Write-Host "[*] Verifying drive mapping"
Start-Sleep -Seconds 2
$driveCheck = Get-WmiObject -Class Win32_LogicalDisk | Where-Object { $_.DeviceID -eq $driveLetter }
if ($driveCheck -and $driveCheck.DriveType -eq 4) {
Write-Host "[OK] Network drive R: successfully mapped!"
Write-Host " Device: $($driveCheck.DeviceID)"
Write-Host " Provider: $($driveCheck.ProviderName)"
Write-Host " Type: Network Drive"
# Test accessibility
try {
if (Test-Path "${driveLetter}\") {
Write-Host "[OK] Drive R: is accessible"
# List contents
$items = Get-ChildItem "${driveLetter}\" -ErrorAction SilentlyContinue
if ($items) {
Write-Host " Contents: $($items.Count) items found"
Write-Host " Sample items:"
$items | Select-Object -First 3 | ForEach-Object {
Write-Host " - $($_.Name) ($($_.Mode))"
}
} else {
Write-Host " Directory appears empty"
}
} else {
Write-Host "[!] Drive mapped but not accessible"
}
} catch {
Write-Host "[!] Error testing accessibility: $($_.Exception.Message)"
}
} else {
Write-Host "[!] Drive mapping verification failed"
# Show current network drives
$networkDrives = Get-WmiObject -Class Win32_LogicalDisk | Where-Object { $_.DriveType -eq 4 }
if ($networkDrives) {
Write-Host "Current network drives:"
foreach ($drive in $networkDrives) {
Write-Host " $($drive.DeviceID) -> $($drive.ProviderName)"
}
} else {
Write-Host "No network drives currently mapped"
}
Write-Host ""
Write-Host "Manual verification:"
Write-Host "1. Open File Explorer"
Write-Host "2. Check if R: drive appears under 'This PC'"
Write-Host "3. Try accessing \\192.168.88.231\RD directly"
}
Write-Host ""
Write-Host "[*] Drive mapping process completed"
# Clean up the desktop batch file since we don't need credentials
$batchFile = "C:\Users\User\Desktop\Map-Drive-R.bat"
if (Test-Path $batchFile) {
Remove-Item $batchFile -Force -ErrorAction SilentlyContinue
Write-Host " Removed credential batch file (not needed)"
}