Last active
September 12, 2024 02:26
-
-
Save bnjdg/bbab37c20bcfe222eb52f5f35b75bf31 to your computer and use it in GitHub Desktop.
Automount network script for windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$driveLetter = "X" | |
$networkPath = "\\172.16.33.8\mnt\lustre\coe-lstr\home\acdalagan" | |
$folderName = "acdalagan" | |
function Rename-NetworkDrive { | |
param ( | |
[string]$driveLetter, | |
[string]$newName | |
) | |
$shell = New-Object -ComObject Shell.Application | |
$drive = $shell.Namespace($driveLetter + ":\") | |
if ($drive) { | |
$drive.Self.Name = $newName | |
Write-Output "Drive $driveLetter renamed to $newName." | |
} else { | |
Write-Output "Failed to rename drive $driveLetter." | |
} | |
} | |
# Check if the drive letter exists | |
if (-Not (Get-PSDrive -Name $driveLetter -ErrorAction SilentlyContinue)) { | |
Write-Output "Drive $driveLetter does not exist. Attempting to mount..." | |
New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $networkPath -Persist | |
if ($?) { | |
Write-Output "Drive $driveLetter successfully mounted." | |
# Add a delay to ensure the drive is properly mounted | |
Start-Sleep -Seconds 5 | |
Rename-NetworkDrive -driveLetter $driveLetter -newName $folderName | |
} else { | |
Write-Output "Failed to mount drive $driveLetter." | |
} | |
} else { | |
Write-Output "Drive $driveLetter already exists." | |
Rename-NetworkDrive -driveLetter $driveLetter -newName $folderName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment