diff --git a/Tools.ps1 b/Tools.ps1 index 5ad6d00..01be0c2 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -1092,6 +1092,40 @@ Function Source-PSScript ([string]$ScriptName) { If ( Test-Path $ScriptFile ) { Write-Output "Removing ${ScriptFile}" ; Remove-Item $ScriptFile -Force -ErrorAction SilentlyContinue } } +# Forcefully maps a network drive but requires user's credentials to already exist in Credential Manager +Function Create-NetworkDrive { + param ( + [Parameter(Mandatory)] + [ValidatePattern("^[A-Z]:$")] + [string]$DriveLetter, + [Parameter(Mandatory)] + [string]$Path, + [Parameter(Mandatory)] + [string]$Label + ) + $existingDrive = Get-CimInstance Win32_LogicalDisk | Where-Object { $_.DeviceID -eq $DriveLetter } + if (-not $existingDrive) { + Write-Output "Mapping ${Path} to drive letter ${DriveLetter}" + cmd.exe /c "net use $DriveLetter $Path /persistent:yes" | Out-Null + } + elseif ($existingDrive.ProviderName -ne $Path) { + Write-Output "Drive exists but points to a different path. Remapping..." + cmd.exe /c "net use $DriveLetter /delete /y" | Out-Null + cmd.exe /c "net use $DriveLetter $Path /persistent:yes" | Out-Null + } + else { Write-Verbose "Drive already mapped correctly." } + Start-Sleep -Seconds 2 + try { + $dl = $DriveLetter.TrimEnd(':') + $vol = Get-Volume -DriveLetter $dl -ErrorAction Stop + if ($vol.FileSystemLabel -ne $Label) { + Write-Output "Setting ${DriveLetter} drive label to ""${Label}""" + Set-Volume -DriveLetter $dl -NewFileSystemLabel $Label + } + } + catch { Write-Verbose "Could not set volume label (possibly unsupported context)." } +} + # Function to import the Syncro Module Function Import-RMMModule () { Try { Import-Module $env:SyncroModule -WarningAction SilentlyContinue -ErrorAction Stop }