I tried to automatically map drive to users using a PowerShell script.
The script create the user with the command:
New-ADUser -Name $userName -GivenName $userName -Surname $userName -DisplayName $userName -Path "OU=Eng,DC=lovely,DC=Local" -SamAccountName $userName -AccountPassword (ConvertTo-SecureString $pass -AsPlainText -Force) -UserPrincipalName ($userName + '@lovely.local') -Enable $true -HomeDrive 'H:' -HomeDirectory "\\DC01\Private\$userName"
the user is created, but when I log on to the user account the drive isn't mapped, the user folder inside the "private" share isn't created.
Then I tried to manually map it from the client and I get this error message:
The mapped drive could not be created because the following error has occurred: The specified network resource or drive is no longer available
So I created the user folder in the server (path: C:\Private\user1
) and I can map it manually.
So I disconnected the drive, and opened the user profile tab (AD Users and Computers → OU → user1 → profile) and manually typed again the same path:
\\DC01\Private\user1
and the drive is mapped once I log on again!
Why is that happening?
- The server (2016 standard) is installed as VM on VirtualBox, the client is Windows 8, also a VM.
- Windows firewall is disabled, also Windows Defender.
- The Windows 8 machine is a member in the domain.
The "Private" share properties:
And again, when I create a new user manually the mapping process is working just fine.
The complete Script:
Import-Module ActiveDirectory
#-----------------#
# Global Var
#-----------------#
$pass = 'Pa$$w0rd'
$drive_letter = 'H:'
$dir_path = '\\DC01\Private'
#-----------------#
# Eng department
#-----------------#
$totalusers = 9
$uname = "Eng"
$ou = "Eng"
for ($i=0; $i -lt $totalusers; $i++) {
$userID = "{0:00}" -f ($i + 1)
$userName = "$uname$userID"
Write-Host "Creating AD user" ($i + 1) "of" $totalusers ":" $userName
New-ADUser -Name $userName -DisplayName $userName -Path "OU=$ou,DC=lovely,DC=Local" -SamAccountName $userName -AccountPassword (ConvertTo-SecureString $pass -AsPlainText -Force) -UserPrincipalName ($userName + '@lovely.local') -HomeDrive $drive_letter -HomeDirectory "$dir_path\$userName" -Enable $true
}