本文目录导读:

我来帮你编写一个网络盘符管理脚本,支持Windows系统下的网络驱动器映射和断开操作。
Windows Batch 脚本
基础版 - 映射和断开网络驱动器
@echo off
chcp 65001 >nul网络盘符管理工具
:menu
cls
echo ========================================
echo 网络盘符管理脚本
echo ========================================
echo.
echo 1. 映射网络驱动器
echo 2. 断开网络驱动器
echo 3. 查看已映射的驱动器
echo 4. 批量映射(从配置文件)
echo 5. 退出
echo.
set /p choice="请选择操作 (1-5): "
if "%choice%"=="1" goto map
if "%choice%"=="2" goto unmap
if "%choice%"=="3" goto list
if "%choice%"=="4" goto batch
if "%choice%"=="5" exit
:map
cls
echo ====== 映射网络驱动器 ======
echo.
set /p drive="请输入盘符 (如 Z): "
set /p path="请输入网络路径 (如 \\server\share): "
set /p user="请输入用户名 (留空使用当前用户): "
set /p pass="请输入密码 (留空使用当前用户): "
if not "%user%"=="" (
if "%pass%"=="" (
net use %drive%: %path% /user:%user% /persistent:yes
) else (
net use %drive%: %path% /user:%user% %pass% /persistent:yes
)
) else (
net use %drive%: %path% /persistent:yes
)
if %errorlevel%==0 (
echo 映射成功!
) else (
echo 映射失败,请检查路径和权限。
)
pause
goto menu
:unmap
cls
echo ====== 断开网络驱动器 ======
echo.
set /p drive="请输入要断开的盘符 (如 Z): "
net use %drive%: /delete
if %errorlevel%==0 (
echo 断开成功!
) else (
echo 断开失败,请检查盘符是否正确。
)
pause
goto menu
:list
cls
echo ====== 当前网络驱动器 ======
echo.
net use
echo.
pause
goto menu
:batch
cls
echo ====== 批量映射 ======
echo 从配置文件 drives.txt 读取配置
echo 格式: 盘符 网络路径 用户名 密码
echo 示例: Z \\server\share user pass
echo.
if not exist drives.txt (
echo 配置文件 drives.txt 不存在!
echo 创建示例配置...
(
echo Z: \\192.168.1.100\share1 user1 pass1
echo Y: \\192.168.1.100\share2 user2 pass2
) > drives.txt
echo 已创建 drives.txt,请编辑后重新运行。
) else (
for /f "tokens=1,2,3,4" %%a in (drives.txt) do (
echo 正在映射 %%a: 到 %%b ...
if "%%c"=="" (
net use %%a %%b /persistent:yes
) else (
net use %%a %%b /user:%%c %%d /persistent:yes
)
)
)
pause
goto menu
PowerShell 脚本
功能更强大的版本
# 网络盘符管理脚本
# 文件名: NetworkDriveManager.ps1
# 设置编码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
function Show-Menu {
Clear-Host
Write-Host "=" * 50
Write-Host " 网络盘符管理工具 (PowerShell)"
Write-Host "=" * 50
Write-Host ""
Write-Host "1. 映射网络驱动器"
Write-Host "2. 断开网络驱动器"
Write-Host "3. 查看所有已映射的驱动器"
Write-Host "4. 断开所有网络驱动器"
Write-Host "5. 测试网络连接"
Write-Host "6. 导出当前配置"
Write-Host "7. 退出"
Write-Host ""
}
function Map-NetworkDrive {
Clear-Host
Write-Host "映射网络驱动器"
Write-Host "-" * 30
$drive = Read-Host "请输入盘符 (如 Z)"
$path = Read-Host "请输入网络路径 (如 \\server\share)"
$persistent = Read-Host "是否持久化?(Y/N, 默认Y)"
if ($persistent -eq "n" -or $persistent -eq "N") {
$persist = $false
} else {
$persist = $true
}
$useCredential = Read-Host "是否使用不同凭据?(Y/N, 默认N)"
if ($useCredential -eq "y" -or $useCredential -eq "Y") {
$cred = Get-Credential -Message "请输入网络凭据"
try {
New-PSDrive -Name $drive -PSProvider FileSystem -Root $path `
-Credential $cred -Persist:$persist -ErrorAction Stop
Write-Host "映射成功!" -ForegroundColor Green
}
catch {
Write-Host "映射失败: $($_.Exception.Message)" -ForegroundColor Red
}
} else {
try {
New-PSDrive -Name $drive -PSProvider FileSystem -Root $path `
-Persist:$persist -ErrorAction Stop
Write-Host "映射成功!" -ForegroundColor Green
}
catch {
Write-Host "映射失败: $($_.Exception.Message)" -ForegroundColor Red
}
}
Read-Host "按Enter继续"
}
function Remove-NetworkDrive {
Clear-Host
$drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -like "\\*" }
if ($drives.Count -eq 0) {
Write-Host "没有已映射的网络驱动器。" -ForegroundColor Yellow
Read-Host "按Enter继续"
return
}
Write-Host "当前网络驱动器:"
$drives | Format-Table Name, Root, Used, Free
$drive = Read-Host "请输入要断开的盘符 (如 Z)"
Remove-PSDrive -Name $drive -Force
Write-Host "断开成功!" -ForegroundColor Green
Read-Host "按Enter继续"
}
function Show-NetworkDrives {
Clear-Host
Write-Host "当前网络驱动器列表:" -ForegroundColor Cyan
Write-Host "-" * 50
$drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -like "\\*" }
if ($drives.Count -eq 0) {
Write-Host "没有已映射的网络驱动器。" -ForegroundColor Yellow
} else {
$drives | ForEach-Object {
Write-Host "$($_.Name): -> $($_.Root)"
}
}
Read-Host "`n按Enter继续"
}
function Remove-AllNetworkDrives {
Clear-Host
Write-Host "将断开所有网络驱动器..." -ForegroundColor Yellow
$drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -like "\\*" }
foreach ($drive in $drives) {
Write-Host "断开 $($drive.Name)..."
Remove-PSDrive -Name $drive.Name -Force
}
Write-Host "所有网络驱动器已断开。" -ForegroundColor Green
Read-Host "按Enter继续"
}
function Test-NetworkConnection {
Clear-Host
$path = Read-Host "请输入要测试的网络路径 (如 \\server\share)"
if (Test-Path $path) {
Write-Host "连接成功!" -ForegroundColor Green
$files = Get-ChildItem $path -File | Select-Object -First 5
if ($files) {
Write-Host "`n前5个文件:"
$files | ForEach-Object { Write-Host " $($_.Name)" }
}
} else {
Write-Host "连接失败!请检查网络路径是否正确。" -ForegroundColor Red
}
Read-Host "`n按Enter继续"
}
function Export-Configuration {
Clear-Host
$filename = "network_drives_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"
$drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Root -like "\\*" }
if ($drives.Count -eq 0) {
Write-Host "没有网络驱动器可导出。" -ForegroundColor Yellow
} else {
$drives | ForEach-Object {
"$($_.Name): $($_.Root)" | Out-File -FilePath $filename -Append
}
Write-Host "配置已导出到: $filename" -ForegroundColor Green
}
Read-Host "按Enter继续"
}
# 主循环
do {
Show-Menu
$choice = Read-Host "请选择操作 (1-7)"
switch ($choice) {
"1" { Map-NetworkDrive }
"2" { Remove-NetworkDrive }
"3" { Show-NetworkDrives }
"4" { Remove-AllNetworkDrives }
"5" { Test-NetworkConnection }
"6" { Export-Configuration }
"7" { Write-Host "退出程序..."; break }
default { Write-Host "无效选择!" -ForegroundColor Red; Read-Host "按Enter继续" }
}
} while ($choice -ne "7")
使用说明
配置示例 (drives.txt)
Z: \\192.168.1.100\共享文件夹 user password Y: \\nas\backup admin pass123 X: \\server\public
运行方法
Batch脚本:
# 直接双击运行或命令行执行 network_drive_manager.bat
PowerShell脚本:
# 如果执行策略限制,先运行: Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass # 运行脚本 .\NetworkDriveManager.ps1
常见问题
- 权限问题: 确保有足够的网络访问权限
- 路径格式: 使用UNC路径格式
\\服务器\共享文件夹 - 防火墙: 确保防火墙允许文件和打印机共享
- 凭据保存: 使用
/persistent:yes参数保存凭据
这个脚本可以根据你的具体需求进行修改和扩展!