为什么我不能将驱动器映射到由 PowerShell 脚本创建的用户

Why can't I map drive to users that was created by PowerShell script

我尝试使用 PowerShell 脚本自动将驱动器映射到用户。

脚本使用以下命令创建用户:

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"

用户已创建,但当我登录到用户帐户时,驱动器未映射,"private" 共享内的用户文件夹未创建。

然后我尝试从客户端手动映射它,但收到此错误消息:

The mapped drive could not be created because the following error has occurred: The specified network resource or drive is no longer available

所以我在服务器中创建了用户文件夹(路径:C:\Private\user1),我可以手动映射它。

所以我断开了驱动器,打开了用户配置文件选项卡(AD 用户和计算机 → OU → user1 → 配置文件)并再次手动输入相同的路径:

\DC01\Private\user1

一旦我再次登录,驱动器就会被映射!

为什么会这样?

同样,当我手动创建新用户时,映射过程工作正常。

完整脚本:

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
}

正如 Rohin Sidharth 和 eckes 在评论中所写,当我在我的脚本中为每个用户创建目录时,问题就解决了。 GUI 有一些功能可以在用户第一次登录时创建文件夹。

现在每个登录的用户都可以自动看到他的主文件夹

编辑:

我添加了一个 for 循环来创建每个部门目录。现在每个用户只能访问他的目录,在目录中有部门名称(并且只有部门用户可以访问该目录)。

foreach ($o in $ous){

Write-Host "Creating OU: " $o
NEW-ADOrganizationalUnit $o

Write-Host "Create Group $o"
New-ADGroup -Name "$o" -SamAccountName $o -GroupCategory Security -GroupScope Global -DisplayName "$o" -Path "CN=Users,DC=lovely,DC=local" -Description "$o department"  

# Create department dir
New-Item -Path "$dir$o" -ItemType Directory   

$colRights = [System.Security.AccessControl.FileSystemRights]"Read, Write,Traverse"
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None 
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly 
$objType =[System.Security.AccessControl.AccessControlType]::Allow 
$objUser = New-Object System.Security.Principal.NTAccount("$o") 
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType) 
$objACL = Get-ACL "$dir$o" 
$objACL.AddAccessRule($objACE) 
Set-ACL "$dir$o" $objACL

}

这里我创建一个部门的用户,例如:

$totalusers = 6
$uname = "Manager"
$ou = "Projects"
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

# create user folder inside the share
CreateUserHomeDir -dir $dir -ou $ou -userName $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') -Enable $true -HomeDrive $drive_letter -HomeDirectory "$dir_path$ou$userName"


SetDirPermissions -ou $ou -userName $userName -dir $dir 

# add to group
AddToGroup -groupName $ou -userName $userName
}

函数:

function AddToGroup ($groupName, $userName)
 {
 Add-ADGroupMember $groupName $userName
}
function CreateUserHomeDir ($dir, $ou, $userName) {

New-Item -Path "$dir$ou$userName" -ItemType Directory
}

function SetDirPermissions ($ou,$userName,$dir) {
    $colRights = [System.Security.AccessControl.FileSystemRights]"Read, Write,Traverse"
    $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None 
    $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly 
    $objType =[System.Security.AccessControl.AccessControlType]::Allow 
    $objUser = New-Object System.Security.Principal.NTAccount("$userName") 
    $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType) 
    $objACL = Get-ACL "$dir$ou$userName" 
    $objACL.AddAccessRule($objACE) 
    Set-ACL "$dir$ou$userName" $objACL

}