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

98 lines
3.2 KiB
PowerShell

[Console]::OutputEncoding = [Text.Encoding]::UTF8
$ErrorActionPreference = 'Continue'
Write-Host "[*] Mapping network drive R: with persistent connection"
$networkPath = "\\192.168.88.231\RD"
$driveLetter = "R:"
Write-Host "[*] Creating persistent network drive mapping"
Write-Host " Target: $networkPath"
Write-Host " Drive: $driveLetter"
# Method 1: Try mapping without credentials first (anonymous/guest access)
Write-Host "[*] Attempting anonymous mapping..."
try {
$result = & net use $driveLetter $networkPath /persistent:yes 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Host "[OK] Anonymous mapping successful!"
} else {
Write-Host " Anonymous mapping failed, will require manual credentials"
}
} catch {
Write-Host " Exception during anonymous mapping"
}
# Check if mapping was successful
$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 " Provider: $($driveCheck.ProviderName)"
# Test access
try {
$testAccess = Test-Path "${driveLetter}\"
if ($testAccess) {
Write-Host "[OK] Drive R: is accessible"
# Try to list contents
$items = Get-ChildItem "${driveLetter}\" -ErrorAction SilentlyContinue | Select-Object -First 3
if ($items) {
Write-Host " Found $($items.Count) items in the drive"
} else {
Write-Host " Drive appears empty or no read permission"
}
} else {
Write-Host "[!] Drive R: mapped but not accessible"
}
} catch {
Write-Host "[!] Cannot test drive access: $($_.Exception.Message)"
}
} else {
Write-Host "[!] Network drive mapping incomplete"
Write-Host ""
Write-Host "Manual mapping required:"
Write-Host "1. Open File Explorer"
Write-Host "2. Right-click 'This PC' > Map network drive"
Write-Host "3. Drive: R:"
Write-Host "4. Folder: \\192.168.88.231\RD"
Write-Host "5. Check 'Reconnect at sign-in'"
Write-Host "6. Enter credentials when prompted"
Write-Host ""
Write-Host "Or use command line:"
Write-Host "net use R: \\192.168.88.231\RD /user:username /persistent:yes"
}
# Create a batch file for easy manual mapping
$batchContent = @"
@echo off
echo Mapping network drive R: to \\192.168.88.231\RD
echo.
echo If prompted, enter the username and password for 192.168.88.231
echo.
net use R: \\192.168.88.231\RD /persistent:yes
echo.
if %errorlevel%==0 (
echo Success! Drive R: mapped successfully.
explorer R:\
) else (
echo Mapping failed. Please check credentials and network connectivity.
)
pause
"@
$batchPath = "C:\Users\User\Desktop\Map-Drive-R.bat"
$batchContent | Out-File -FilePath $batchPath -Encoding ASCII
Write-Host "[*] Created manual mapping batch file: Map-Drive-R.bat"
Write-Host ""
Write-Host "Network drive setup complete!"
Write-Host ""
Write-Host "If R: drive is not visible:"
Write-Host "1. Double-click 'Map-Drive-R.bat' on User's desktop"
Write-Host "2. Enter network credentials when prompted"
Write-Host "3. Drive R: will appear in File Explorer"