开发手记(七)——Windows设置磁盘共享

问题描述:所有服务器都是Windows系统并且使用默认的Administrator用户,源服务器一台,目标服务器N台,所有服务器的IP地址固定不变。源服务器Y盘共享,目标服务器没有Y盘冲突。目标服务器可连接源服务器的445端口(SMB协议),目标服务器重启也无需再次修复共享。

1. 配置源服务器

首先确保源服务器有Y盘,没有就新建分区。

打开“此电脑”或“文件资源管理器”,找到磁盘Y。右键点击磁盘Y,选择“属性”,找到“共享”选项卡,点击“高级共享”。

image

 勾选“共享此文件夹”,填写“共享名”为Y$(Y也可以,$的作用是隐藏,不显示在网络邻居),用户数量限制的最小值设置为目标服务器的个数,再点击“权限”按钮。

image

 在权限弹窗勾选给“完全控制”,也可以根据业务需求进行调整。最后,一直确定所有弹窗。

image

 

2. 配置目标服务器

目标服务器一步步设置有些复杂,这里我们直接改用powershell脚本实现自动操作。配置区域的内容需要仔细核对。

# Fix-YDrive.ps1
# 功能:自动修复 Y: 盘映射

# ================= 1. 配置区域 (根据需求修改) =================
$SourceIP = "xxx.xxx.xxx.xxxx"       # 源服务器 IP
$ShareName = "Y$"                 # 【关键】使用 Y$ (管理共享),根据 net share 结果确认
                                  # 如果 Y$ 不行,可改为 "Y"
$DriveLetter = "Y:"               # 本地映射盘符
$TargetSubDir = "directory"   # 需要访问的子文件夹名称

# 认证信息
$Username = "Administrator"
$Password = "your-password"       

# 自动拼接路径
$ShareRoot = "\\$SourceIP\$ShareName"
$FullTargetPath = "$DriveLetter\$TargetSubDir"
# =======================================================

Write-Host "----------------------------------------" -ForegroundColor Cyan
Write-Host "开始检查 $DriveLetter 盘连接状态..." -ForegroundColor Cyan
Write-Host "目标路径: $ShareRoot" -ForegroundColor Gray

# 1. 获取当前映射信息
$Mapping = Get-SmbMapping -LocalPath $DriveLetter -ErrorAction SilentlyContinue

$NeedRepair = $false
$Reason = ""

# 2. 诊断逻辑
if (-not $Mapping) {
    $Reason = "盘符未映射"
    $NeedRepair = $true
}
elseif ($Mapping.RemotePath -ne $ShareRoot) {
    $Reason = "映射路径错误 (当前: $($Mapping.RemotePath), 期望: $ShareRoot)"
    $NeedRepair = $true
}
elseif ($Mapping.Status -ne 'OK') {
    $Reason = "连接状态异常 (状态: $($Mapping.Status))"
    $NeedRepair = $true
}
else {
    # 路径看起来对了,进一步测试是否能访问子目录
    if (-not (Test-Path $FullTargetPath)) {
        $Reason = "盘符存在但无法访问子目录 '$TargetSubDir'"
        $NeedRepair = $true
    }
}

# 3. 执行修复
if ($NeedRepair) {
    Write-Host "❌ 检测到问题:$Reason" -ForegroundColor Yellow
    Write-Host "🔄 正在执行修复流程..." -ForegroundColor Cyan

    # A. 强制断开旧连接
    Write-Host "   [1/3] 清理旧映射..."
    net use $DriveLetter /delete /y 2>$null
    Remove-SmbMapping -LocalPath $DriveLetter -Force -ErrorAction SilentlyContinue
    Start-Sleep -Seconds 2

    # B. 建立新连接
    Write-Host "   [2/3] 正在映射:$ShareRoot ..."
    
    # 构造 net use 命令
    # 格式:net use <盘符> <路径> <密码> /user:<用户名> /persistent:yes
    $Cmd = "net use $DriveLetter `"$ShareRoot`" `"$Password`" /user:`"$Username`" /persistent:yes"
    
    # 执行命令
    Invoke-Expression $Cmd
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host "   [3/3] 验证连接..."
        Start-Sleep -Seconds 2
        
        if (Test-Path $FullTargetPath) {
            Write-Host "✅ 成功!$DriveLetter 已正确映射并可访问。" -ForegroundColor Green
            Write-Host "   完整路径: $FullTargetPath" -ForegroundColor Green
        } else {
            Write-Host "⚠️ 警告:映射成功,但找不到子目录 '$TargetSubDir'。" -ForegroundColor Yellow
            Write-Host "   请检查源服务器上是否存在路径:Y:\$TargetSubDir" -ForegroundColor Yellow
            Write-Host "   当前可访问根目录,您可以手动查看。" -ForegroundColor Gray
        }
    } else {
        Write-Host "❌ 失败:无法建立网络连接。" -ForegroundColor Red
        Write-Host "   错误代码:$LASTEXITCODE" -ForegroundColor Red
        Write-Host "   建议检查:" -ForegroundColor Gray
        Write-Host "   1. 源服务器防火墙是否关闭?" -ForegroundColor Gray
        Write-Host "   2. 共享名是否正确?(当前尝试使用:$ShareName)" -ForegroundColor Gray
        Write-Host "   3. 账号密码是否正确?" -ForegroundColor Gray
        exit 1
    }
} else {
    Write-Host "✅ 状态正常:$DriveLetter 已正确连接至 $ShareRoot" -ForegroundColor Green
    Write-Host "   无需操作。" -ForegroundColor Gray
}

Write-Host "----------------------------------------" -ForegroundColor Cyan

 保存文件后,在目标服务器上运行命令。因为要实现自启动,所以该命令可以设置为Windows计划任务 → 开发手记(一)——Windows设置开机自启动后台服务 - 学术大垃圾 - 博客园

powershell.exe -ExecutionPolicy Bypass -File "absolute\path\to\Fix-YDrive.ps1"
posted @ 2026-03-18 14:04  学术大垃圾  阅读(17)  评论(0)    收藏  举报