Home > other >  Check drive letter map and replace it only if it doesn't match (Powershell)
Check drive letter map and replace it only if it doesn't match (Powershell)

Time:10-20

In powershell, how to check drive letter mapping and replace it only if it doesn't match the path, I don't want to delete it and add it again because this could kill other processes that are using the drive letter, but I don't mind checking it everytime.

Set-MapDrive "Z:" "//MyServer/Stuff1"

Here's what I have so far.

How to implement the Get-DriveMap Function?

function Set-MapDrive {
    param(
        [string]$letter, 
        [string]$shar_path
    )

    $curr_path = Get-DriveMap($letter)

    # Letter Not Mapped
    if ($curr_path -eq $null) {
        net use $letter $share_path
    }
    else {
        $dir1 = Get-Item $user_path
        $dir2 = Get-Item $share_path

        # Letter Map has changed
        if($dir1.GetHashCode() -eq $dir2.GetHashCode()) {
            net use $letter \delete
            net use $letter $share_path
        }

        # No Change
        else {
             write-host "Note: Driver $letter already mapped to $share_path"
        }
    }
}
function Get-DriveMap {
    param(
        [string]$letter
    )
     
    $x = Get-PSDrive $letter
    #^^^^ This produces error if letter doesn't exist?!
    # need it to set $x to null if it doesn't exist.

    return $x.DisplayRoot
}

CodePudding user response:

Use Get-PSDrive to fetch any existing drive:

function Set-MapDrive {
  param(
    [string]$Letter,
    [string]$RootPath
  )

  $existingDrive = Get-PSDrive -Name $Letter.TrimEnd(':') -ErrorAction SilentlyContinue
  if($existingDrive){
    if($existingDrive.Root -eq $RootPath){
      # nothing more to do, drive already exists with correct root path
      return
    }

    # remove existing drive mapping here
  }

  # create new drive mapping here
}

CodePudding user response:

function my_drive_map {
    param(
        [string]$letter, 
        [string]$share_path
    )
    #example: my_map_drive "Z:" "\\MyServer\MyDir"

    write-host "my_drive_map $letter $share_path"

    $L  = $letter.TrimEnd(':')
    $LL = "${L}:"

    if (-not (Test-Path $share_path)) {
        write-host "    ERROR: Network share path is inaccessable"
        exit 1
    }

    $map = Get-SmbMapping -LocalPath $LL -ErrorAction SilentlyContinue  
            
    #$map | select *

    if ($map -eq $null) {
            write-host "    # case1: Drive Not Mapped Yet"
            write-host "    PS> Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue (case3)"
            Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue | out-null

            write-host "    PS> New-SmbMapping -LocalPath $LL -RemotePath $share_path -Persistent"
            New-SmbMapping -LocalPath $LL -RemotePath $share_path -Persistent $true | out-null
    }
    
    # Drive-Letter Already Mapped
    else  {    
        $dir1 = $map.RemotePath
        $dir2 = $share_path
      

        # Drive-Letter Map has changed, Remap it
        if($dir1 -ne $dir2) {
            write-host "    # case2: Drive-Letter already mapped to wrong Path, Change it"
            write-host "    # comparing path1($dir1) to path2($dir2)"
                
            write-host "    PS> Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue | out-null"
            Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue | out-null        

            write-host "    PS> New-SmbMapping -LocalPath $LL -RemotePath $share_path -Persistent"
            New-SmbMapping -LocalPath $LL -RemotePath $share_path -Persistent $true | out-null
        }
        else {
            write-host "    # case3: Drive-Letter already mapped and is correct"
            write-host "    # comparing path1($dir1) to path2($dir2)"
        }
    }       
    
    # Check that Drive-Letter is available
    if (-not (Test-Path "${L}:\")) {
        write-host "    ERROR: Drive-Letter ${L}: is still inaccessable"
        exit 1
    }
    
    write-host  "    (OK) ${L}: accessable as $share_path"
}

function my_drive_list {
    Get-SmbMapping
}

function my_drive_remove {
    param(
        [string]$letter
    )

    write-host "my_drive_remove $letter $share_path"

    $L  = $letter.TrimEnd(':')
    $LL = "${L}:"

    Remove-SmbMapping -LocalPath $LL -Force -ErrorAction SilentlyContinue | out-null
    
    $map = Get-SmbMapping -LocalPath $LL -ErrorAction SilentlyContinue  
            
    if ($map -eq $null) {
        write-host "    (OK) Drive sucessfully removed"
    }
    else {
        write-host "    (err) Drive still attached"
    }
    
}
  • Related