Posted On September 9, 2020

Quick Script to Mount Remote UNC Paths as Local Drive Letters

kimconnect 0 comments
blog.KimConnect.com >> Codes >> Quick Script to Mount Remote UNC Paths as Local Drive Letters

Simple Mount:

# Mapping a drive
$username='domain\testAdmin'
$password='PASSWORD'
$remotePath='\\UNCSERVER\SHAREPATH\Backup'
$unavailableDriveLetters=(Get-Volume).DriveLetter|sort
$availableDriveLetters=.{(65..90|%{[char]$_})|?{$_ -notin $unavailableDriveLetters}}
[char]$firstAvailableDriveLetter=$availableDriveLetters[0]
$command="net use $firstAvailableDriveLetter`: $remotePath $password /USER:$username /persistent:Yes"
write-host $command
Invoke-Expression $command

# Copying a file
$localFile='X:\backup\TEST_DB.bak'
robocopy $(split-path $localFile -parent) "$firstAvailableDriveLetter`:\" $(split-path $localFile -leaf)

# Removing mapped drive
net use /del "$firstAvailableDriveLetter`:" /y

Copying a Script File:

$remoteFile='\\REMOTESERVER\C$\Scripts\somescripts.ps1'
$localPath='C:\Scripts'
$remoteAdminUser='REMOTESERVER\Admin'
$remoteAdminPass='WHATPASSWORD'
$remotePath=split-path $remoteFile -parent

# Try to copy file from remote UNC path to Local. If that fails, then perform the mount drive method
try{
    Import-Module bitstransfer
    Start-BitsTransfer -Source $scriptFile -Destination $localPath -Credential $adminCredential -ea Stop
}catch{
    # Option 1: deterministic method of discovering unavailable drive letters
    # $unavailableDriveLetters=(Get-Volume).DriveLetter|sort # This only works on Windows 8/2012 or higher
    $unavailableDriveLetters=(Get-WmiObject -Class Win32_Volume).DriveLetter|sort
    $availableDriveLetters=.{(65..90|%{[char]$_})|?{"$_`:" -notin $unavailableDriveLetters}}
    [char]$firstAvailableDriveLetter=$availableDriveLetters[0]
    net use $firstAvailableDriveLetter $remotePath /USER:$user "$password" /persistent:Yes 2>&1>null
    Copy-Item $remoteFile $localPath 
}

# Option 2: Blanket test path approach, which will yield False Positives with read-only drives such as CD-ROM
# This is backward compatible to PowerShell 3.0
#[char]$firstAvailableDriveLetter=.{$letters=(65..90|%{[char]$_});foreach ($letter in $letters){if(!(test-path "$letter`:")){return $letter} }}
#net use $firstAvailableDriveLetter $remotePath /USER:$user "$password" /persistent:Yes 2>&1>null

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Install TightVNC on Windows via CLI

Install VNC Server on the Remote Windows: # Access Remote Server via WinRM$SHERVERNAME = "SHERVER007"$winRMHttp=5985$winRMHttps=5986#…

PowerShell: Create Daily VSS Snapshot of Volumes on Local Windows Machine

<# Daily-VSS-Snapshot-Windows-Standalone-FileServer.ps1 Functions: 1. Dynamically detect all volumes on local machine 2. Take snapshots of…

Add Local Windows User

# Dynamic Credential$who = whoamiif ($who.substring($who.length-2, 2) -eq "-admin"){$username=$who;}else {$username=$who+"-admin";}$password = Read-Host -Prompt "Input the…