Intune Drive Mapping Script

Jesse Haswell 20 Reputation points
2025-02-07T20:13:43.5466667+00:00

Hello, I have an issue that I can't find a solution for. I am writing a PowerShell script that will map on-prem network drives to Autopilot devices that become Entra Joined. The plan is to eventually move to strictly Cloud once we can figure out a solution for moving our on-prem shares and getting old school employees onboard. Currently, we are just planning on using Autopilot to deploy remote devices and use SSO to access on-prem resources. I have already confirmed that this is possible when the user connects to our VPN so the only setback is mapping the drives for the user.

---------------------------------------------------------------- The script I wrote is: # Define variables

$driveLetter = "N:"

$networkPath = "\domain\shares"

$logFile = "C:\install.log"

$ErrorActionPreference = 'Inquire'

Function to log messages

function Log-Message {

param (

[string]$message

)

$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

"$timestamp - $message" | Out-File -FilePath $logFile -Append

}

Start logging

Log-Message "Script started."

try {

Map network drive using SSO

Log-Message "Mapping network drive $driveLetter to $networkPath."

$drive = New-PSDrive -Name 'N' -PSProvider FileSystem -Root $networkPath -Persist -Scope Global -ErrorAction Stop -Verbose 4>&1 | Tee-Object -FilePath $logFile -Append

Rename the mapped drive

Log-Message "Renaming mapped drive to 'CustomName'."

$driveInfo = Get-WMIObject -Query "SELECT * FROM Win32_LogicalDisk WHERE DeviceID='$driveLetter'" -ErrorAction Stop -Verbose 4>&1 | Tee-Object -FilePath $logFile -Append

$driveInfo.VolumeName = "CustomName"

$driveInfo.Put() | Out-Null

Add all network printers

Log-Message "Adding all network printers."

$printers = Get-WMIObject -Query "SELECT * FROM Win32_Printer WHERE Network = TRUE" -ErrorAction Stop -Verbose 4>&1 | Tee-Object -FilePath $logFile -Append

foreach ($printer in $printers) {

Add-Printer -ConnectionName $printer.Name -ErrorAction Stop -Verbose 4>&1 | Tee-Object -FilePath $logFile -Append

}

Log-Message "Script completed successfully."

} catch {

Log-Message "An error occurred: $_"

}

End logging

Log-Message "Script ended." ----------------------------------------------------------------------- Interestingly enough, the script seems to work on my work PC which is AD joined and Intune Enrolled but it doesn't work on a AAD joined device using the exact same credentials. The script is set to run with system level permissions and as you can see I have a log to show me what is going on but all it says typically is that access is denied. Here is the log report from the remote device verses the log report from the AD joined device. Both are using my credentials so the user permissions should be the same on both devices: 2025-02-07 08:05:11 - Script started.

2025-02-07 08:05:11 - Mapping network drive N: to \domain\share.

Performing the operation "New drive" on target "Name: N Provider: Microsoft.PowerShell.Core\FileSystem Root:

\domain\share".

2025-02-07 08:05:12 - An error occurred: Access is denied

2025-02-07 08:05:12 - Script ended. ------------------------------------------------------------------------- I am not sure why there would be a difference if the user permissions are the same and the script is running as system anyway. Anything I should try? Thanks in advance for your help!

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,874 questions
Microsoft Intune
Microsoft Intune
A Microsoft cloud-based management solution that offers mobile device management, mobile application management, and PC management capabilities.
5,542 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Crystal-MSFT 51,736 Reputation points Microsoft Vendor
    2025-02-10T02:55:55.7933333+00:00

    @Jesse Haswell, Thanks for posting in Q&A. To map drive for Microsoft Entra joined device. I find two methods:

    1, Use the DriveMapping admx. Rudy has created a blog on how to use it: https://call4cloud.nl/2021/03/willy-wonka-and-the-drive-letter-factory/

    Note: None-Microsoft link, just for the reference.

    2, Utilize a PowerShell script in Intune, that will create a task on the devices. Here is the link: https://intunedrivemapping.azurewebsites.net/

    Note: None-Microsoft link, just for the reference.

    I notice we use PowerShell script, in the above link, it mentions to modify the PowerShell script for a VPN based event trigger. Please try the above method to generate script and modify for VPN trigger to see if it can work.

    Hope the above information can help.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.