0

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
}
pico
  • 1,660
  • 4
  • 22
  • 52
  • As an aside: PowerShell functions, cmdlets, scripts, and external programs must be invoked _like shell commands_ - `foo arg1 arg2` - _not_ like C# methods - `foo('arg1', 'arg2')`. If you use `,` to separate arguments, you'll construct an _array_ that a command sees as a _single argument_. To prevent _accidental_ use of method syntax, use [`Set-StrictMode -Version 2`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher, but note its other effects. See [this answer](https://stackoverflow.com/a/65208621/45375) for more information. – mklement0 Oct 19 '21 at 13:34

2 Answers2

2

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
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0
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"
    }
    
}
pico
  • 1,660
  • 4
  • 22
  • 52