Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update drive mapping issue #31

Open
bjhitm opened this issue Jun 11, 2021 · 5 comments
Open

Update drive mapping issue #31

bjhitm opened this issue Jun 11, 2021 · 5 comments

Comments

@bjhitm
Copy link

bjhitm commented Jun 11, 2021

Hi great script and with the addition from intune.training is working very well for remote users still needing a vpn connection etc.

This has all deployed via Intune\endpoint manager like a dream. I've now changed a couple of UNC paths to use a DFS path. I can see the new DriveMapping.ps1 is deployed correctly but the drive mapping doesn't update. If I manually delete the drive mapping the next time the task runs it will create the mapping using the new correct path so it's just the deletion of the old one not working.

Looking at DriveMapping.log I do see an error but I can't figure out why it's not deleting and re-creating the drive mapping

Mapping network drive \xxxxx.local\xxxxxxData\Store
PS>TerminatingError(New-PSDrive): "The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The local device name is already in use"
C:\ProgramData\intune-drive-mapping-generator\DriveMappping.ps1 : The local device name is already in use
At line:1 char:3

  • &{C:\PROGRA3\INTUNE1\DRIVEM~2.PS1}
  • + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,DRIVEM~2.PS1
    

C:\ProgramData\intune-drive-mapping-generator\DriveMappping.ps1 : The local device name is already in use
At line:1 char:3

  • &{C:\PROGRA3\INTUNE1\DRIVEM~2.PS1}
  • + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,DRIVEM~2.PS1
    
    

Any thoughts?

Thanks
Barry

@DaBXRoMeO
Copy link

DaBXRoMeO commented Sep 20, 2021

I'm not sure if this will help, but I had a similar request where they wanted the path to self correct in the event that a user changed path. I added a block in the script after the comment line 117. It checks to see if the drive letter and path match. If not, it deletes the mapping and the rest of the script continues to create the intended letter\path. Just make sure to update the intended path(s) in the script below. I hope this helps.

if (-not (Test-RunningAsSystem)) {

$driveMappings = Get-WmiObject -Class Win32_mappedLogicalDisk | select name,providername
$Sdrive = "S:"
$Mdrive = "M:"

foreach ($driveMapping in $driveMappings){

    if ($driveMapping.name -match $Sdrive){
        if ($driveMapping.ProviderName -ne '<intended path>') {
            net use S: /delete
            }
    }
    if ($driveMapping.name -match $Mdrive){
        if ($driveMapping.ProviderName -ne '<intended path>') {
            net use M: /delete
            }
    }
}

Start-Sleep -Seconds 10

@lms93
Copy link

lms93 commented Jun 16, 2023

I'm not sure if this will help, but I had a similar request where they wanted the path to self correct in the event that a user changed path. I added a block in the script after the comment line 117. It checks to see if the drive letter and path match. If not, it deletes the mapping and the rest of the script continues to create the intended letter\path. Just make sure to update the intended path(s) in the script below. I hope this helps.

if (-not (Test-RunningAsSystem)) {

$driveMappings = Get-WmiObject -Class Win32_mappedLogicalDisk | select name,providername
$Sdrive = "S:"
$Mdrive = "M:"

foreach ($driveMapping in $driveMappings){

    if ($driveMapping.name -match $Sdrive){
        if ($driveMapping.ProviderName -ne '<intended path>') {
            net use S: /delete
            }
    }
    if ($driveMapping.name -match $Mdrive){
        if ($driveMapping.ProviderName -ne '<intended path>') {
            net use M: /delete
            }
    }
}

Start-Sleep -Seconds 10

Hey @DaBXRoMeO
Did you add this after commented section #Mapping network drives# ?

@DaBXRoMeO
Copy link

I'm not sure if this will help, but I had a similar request where they wanted the path to self correct in the event that a user changed path. I added a block in the script after the comment line 117. It checks to see if the drive letter and path match. If not, it deletes the mapping and the rest of the script continues to create the intended letter\path. Just make sure to update the intended path(s) in the script below. I hope this helps.
if (-not (Test-RunningAsSystem)) {

$driveMappings = Get-WmiObject -Class Win32_mappedLogicalDisk | select name,providername
$Sdrive = "S:"
$Mdrive = "M:"

foreach ($driveMapping in $driveMappings){

    if ($driveMapping.name -match $Sdrive){
        if ($driveMapping.ProviderName -ne '<intended path>') {
            net use S: /delete
            }
    }
    if ($driveMapping.name -match $Mdrive){
        if ($driveMapping.ProviderName -ne '<intended path>') {
            net use M: /delete
            }
    }
}

Start-Sleep -Seconds 10

Hey @DaBXRoMeO Did you add this after commented section #Mapping network drives# ?

Correct, after the comment section that starts "#Mapping network drive#"

@lms93
Copy link

lms93 commented Jun 26, 2023

Thanks @DaBXRoMeO
I've found that with the added code of yours it still will continue to remove and replace the UNC path regardless of it being correct on the client end or not...

@fr3dr1x
Copy link

fr3dr1x commented Oct 30, 2024

# Create a hash table of drives with corrected paths
$drives = @{}
foreach ($drive in $driveMappingConfig) {
    $driveLetter = $drive.DriveLetter + ":"
    $drives[$driveLetter] = $drive.Path -replace "\\\\", "\\"  # Correct double backslashes in each Path here
}

# Loop through each drive and check if it is mapped correctly
foreach ($drive in $drives.GetEnumerator()) {
    $driveLetter = $drive.Key
    $correctPath = $drive.Value

    # Check if the drive is currently mapped
    $mappedDrive = Get-WmiObject -Query "SELECT * FROM Win32_NetworkConnection WHERE LocalName = '$driveLetter'" -ErrorAction SilentlyContinue

    if ($mappedDrive) {
        # If the drive is mapped, check if the UNC path is correct
        if ($mappedDrive.RemoteName -ne $correctPath) {
            Write-Host "The path for $driveLetter is incorrect. Removing drive."
            # Remove the drive if the path is incorrect
            cmd /c "net use $driveLetter /delete /yes"
        } else {
            Write-Host "The path for $driveLetter is correct."
        }
    } else {
        Write-Host "$driveLetter is not mapped."
    }
}

Add this after the JSON part (I dit it on line 37), it will remove a drive if doen't match the path values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants