138 lines
4.6 KiB
PowerShell
138 lines
4.6 KiB
PowerShell
[Console]::OutputEncoding = [Text.Encoding]::UTF8
|
|
$ErrorActionPreference = 'Continue'
|
|
|
|
Write-Host "[*] Setting up network drive R: and removing desktop shortcut"
|
|
|
|
# File paths
|
|
$shortcutPath = "C:\Users\User\Desktop\Network Share (RD).lnk"
|
|
$networkPath = "\\192.168.88.231\rd"
|
|
$driveLetter = "R:"
|
|
|
|
Write-Host "[*] Step 1: Removing desktop shortcut"
|
|
|
|
if (Test-Path $shortcutPath) {
|
|
try {
|
|
Remove-Item $shortcutPath -Force
|
|
Write-Host "[OK] Desktop shortcut removed: Network Share (RD).lnk"
|
|
} catch {
|
|
Write-Host "[!] Failed to remove shortcut: $($_.Exception.Message)"
|
|
}
|
|
} else {
|
|
Write-Host " Desktop shortcut not found (already removed)"
|
|
}
|
|
|
|
Write-Host "[*] Step 2: Checking current network drives"
|
|
|
|
# Check existing drives
|
|
$existingDrives = Get-WmiObject -Class Win32_LogicalDisk | Select-Object DeviceID, DriveType, ProviderName
|
|
$networkDrives = $existingDrives | 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 "[*] Step 3: Mapping network drive R:"
|
|
|
|
# Check if R: is already in use
|
|
$rDriveExists = $existingDrives | Where-Object { $_.DeviceID -eq $driveLetter }
|
|
if ($rDriveExists) {
|
|
Write-Host " R: drive already exists"
|
|
if ($rDriveExists.ProviderName -eq $networkPath) {
|
|
Write-Host "[OK] R: is already mapped to $networkPath"
|
|
exit 0
|
|
} else {
|
|
Write-Host " R: is mapped to: $($rDriveExists.ProviderName)"
|
|
Write-Host " Disconnecting existing mapping..."
|
|
try {
|
|
& net use $driveLetter /delete /yes
|
|
Write-Host " Existing R: mapping removed"
|
|
} catch {
|
|
Write-Host "[!] Failed to disconnect R: drive"
|
|
}
|
|
}
|
|
}
|
|
|
|
Write-Host " Mapping $networkPath to $driveLetter"
|
|
|
|
# Test network connectivity first
|
|
Write-Host "[*] Step 4: Testing network connectivity"
|
|
try {
|
|
$testConnection = Test-NetConnection -ComputerName "192.168.88.231" -Port 445 -WarningAction SilentlyContinue
|
|
if ($testConnection.TcpTestSucceeded) {
|
|
Write-Host "[OK] Network connection to 192.168.88.231:445 successful"
|
|
} else {
|
|
Write-Host "[!] Warning: Cannot reach 192.168.88.231:445"
|
|
}
|
|
} catch {
|
|
Write-Host "[!] Network test failed: $($_.Exception.Message)"
|
|
}
|
|
|
|
# Map the network drive
|
|
Write-Host "[*] Step 5: Creating network drive mapping"
|
|
|
|
try {
|
|
# Use net use command for mapping
|
|
$result = & net use $driveLetter $networkPath 2>&1
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host "[OK] Network drive mapped successfully!"
|
|
Write-Host " $driveLetter -> $networkPath"
|
|
|
|
# Verify the mapping
|
|
Start-Sleep -Seconds 2
|
|
$verification = Get-WmiObject -Class Win32_LogicalDisk | Where-Object { $_.DeviceID -eq $driveLetter }
|
|
if ($verification) {
|
|
Write-Host "[OK] Drive mapping verified"
|
|
Write-Host " Drive type: $($verification.DriveType) (4 = Network)"
|
|
Write-Host " Provider: $($verification.ProviderName)"
|
|
}
|
|
|
|
} else {
|
|
Write-Host "[!] Network drive mapping failed"
|
|
Write-Host " Error output: $result"
|
|
|
|
# Provide troubleshooting info
|
|
Write-Host ""
|
|
Write-Host "Possible solutions:"
|
|
Write-Host "1. Run as Administrator: net use R: \\192.168.88.231\rd"
|
|
Write-Host "2. With credentials: net use R: \\192.168.88.231\rd /user:username"
|
|
Write-Host "3. Check if the share exists and is accessible"
|
|
Write-Host "4. Verify network connectivity to 192.168.88.231"
|
|
}
|
|
|
|
} catch {
|
|
Write-Host "[!] Exception during mapping: $($_.Exception.Message)"
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "[*] Final status check"
|
|
|
|
# Final verification
|
|
$finalDrives = Get-WmiObject -Class Win32_LogicalDisk | Where-Object { $_.DriveType -eq 4 }
|
|
if ($finalDrives) {
|
|
Write-Host "Current network drives:"
|
|
foreach ($drive in $finalDrives) {
|
|
if ($drive.DeviceID -eq $driveLetter) {
|
|
Write-Host "[OK] $($drive.DeviceID) -> $($drive.ProviderName) (SUCCESS)"
|
|
} else {
|
|
Write-Host " $($drive.DeviceID) -> $($drive.ProviderName)"
|
|
}
|
|
}
|
|
} else {
|
|
Write-Host "No network drives mapped"
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "Setup complete!"
|
|
Write-Host "- Desktop shortcut removed"
|
|
Write-Host "- Network drive R: mapping attempted"
|
|
Write-Host ""
|
|
Write-Host "Access the network share via:"
|
|
Write-Host "1. File Explorer > This PC > R: drive"
|
|
Write-Host "2. Address bar: R:\"
|
|
Write-Host "3. Run dialog: R:\" |