You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

请求编写PowerShell脚本重新映射网络共享驱动器

Hey there! Let's work through this network share remapping challenge together—since you're new to PowerShell, I’ll break this down with a practical script and clear explanations so you can get those 50 users switched over smoothly while keeping their drive letters and file structure intact.

PowerShell Script to Remap Network Shares

The goal here is to detect existing mappings to NetworkShare1, safely disconnect them, then re-map the same drive letters to the corresponding paths on NetworkShare2.

Local User Script (Run on Each User's PC)

This script is designed to run directly on a user's machine (you can deploy it via group policy login script, or have users run it manually if needed):

# Replace these with your actual share paths
$oldShareRoot = "\\YourOldServer\NetworkShare1"
$newShareRoot = "\\YourNewServer\NetworkShare2"

# Get all mapped drives tied to the old network share
$mappedDrives = Get-PSDrive -PSProvider FileSystem | Where-Object {
    $_.DisplayRoot -like "$oldShareRoot*"
}

# Loop through each drive to remap
foreach ($drive in $mappedDrives) {
    try {
        # Calculate the relative path from the old share root (e.g., "\C" from "\\OldServer\NetworkShare1\C")
        $relativePath = $drive.DisplayRoot.Substring($oldShareRoot.Length)
        
        # Build the full path for the new share
        $newPath = $newShareRoot + $relativePath
        
        # Disconnect the old drive mapping
        Write-Host "Disconnecting drive $($drive.Name): $($drive.DisplayRoot)"
        Remove-PSDrive -Name $drive.Name -Force -ErrorAction Stop
        
        # Re-map the same drive letter to the new share path
        Write-Host "Mapping drive $($drive.Name) to $newPath"
        New-PSDrive -Name $drive.Name -PSProvider FileSystem -Root $newPath -Persist -ErrorAction Stop
        
        Write-Host "Successfully remapped drive $($drive.Name)`n"
    }
    catch {
        Write-Error "Failed to remap drive $($drive.Name): $_"
    }
}

How This Script Works

  • Detect Mappings: It uses Get-PSDrive to find all file system drives that point to NetworkShare1 (or any subpath under it).
  • Preserve File Structure: By calculating the relative path from the old share root, it ensures the same folder structure is mirrored on NetworkShare2.
  • Safe Remapping: It first removes the old mapping, then creates a new one with the exact same drive letter. The -Persist flag makes sure the mapping stays after the user restarts their PC.

Bulk Remote Execution (For 50 Users)

If you want to run this across all 50 user machines without manual intervention, you can use PowerShell remoting (requires WinRM enabled on target PCs):

# List of user computer names (one per line in the text file)
$computers = Get-Content -Path "C:\Path\To\UserComputers.txt"

# Replace these with your actual share paths
$oldShareRoot = "\\YourOldServer\NetworkShare1"
$newShareRoot = "\\YourNewServer\NetworkShare2"

foreach ($computer in $computers) {
    Write-Host "Processing $computer..."
    Invoke-Command -ComputerName $computer -ScriptBlock {
        param($oldShare, $newShare)
        $mappedDrives = Get-PSDrive -PSProvider FileSystem | Where-Object {
            $_.DisplayRoot -like "$oldShare*"
        }
        foreach ($drive in $mappedDrives) {
            try {
                $relativePath = $drive.DisplayRoot.Substring($oldShare.Length)
                $newPath = $newShare + $relativePath
                Remove-PSDrive -Name $drive.Name -Force -ErrorAction Stop
                New-PSDrive -Name $drive.Name -PSProvider FileSystem -Root $newPath -Persist -ErrorAction Stop
                Write-Host "Successfully remapped $($drive.Name) on $env:COMPUTERNAME"
            }
            catch {
                Write-Error "Failed on $env:COMPUTERNAME for $($drive.Name): $_"
            }
        }
    } -ArgumentList $oldShareRoot, $newShareRoot -ErrorAction Continue
}

Key Notes & Tips

  1. Test First: Run the local script on one or two user machines first to make sure it works as expected and doesn't cause permission issues.
  2. Update Login Scripts: If users have an existing login script that maps NetworkShare1, be sure to update it to point to NetworkShare2—otherwise, the old mappings will come back on next login.
  3. Permissions: Ensure users have the same read/write permissions on NetworkShare2 as they did on NetworkShare1 to avoid access errors.
  4. Admin Rights: You don't need admin rights to run the local script (drive mappings are user-specific), but remote execution requires appropriate permissions to access the target PCs.

内容的提问来源于stack exchange,提问作者Logan Miller

火山引擎 最新活动