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

标签 powershell active-directory windows-server-2016 home-directory network-shares

我尝试使用 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

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

为什么会这样?
  • 服务器(2016 标准)作为虚拟机安装在 VirtualBox 上,客户端是 Windows 8,也是一个虚拟机。
  • Windows 防火墙已禁用,Windows Defender 也已禁用。
  • Windows 8 计算机是域中的成员。
  • “私有(private)”共享属性:

    Share permissions "\DC01\Private": Authenticated Users (Full Control)

    NTFS permissions "C:\Private": SYSTEM (Full Control), Administrators (Full Control), CREATOR OWNER (Full Control, subfolders and files only), Authenticated Users (Full Control)

  • 再一次,当我手动创建一个新用户时,映射过程工作得很好。

    完整的脚本:
    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
    
    }
    

    关于powershell - 为什么我不能将驱动器映射到由 PowerShell 脚本创建的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46394331/

    相关文章:

    powershell - 有没有办法在 PowerShell 中的许多函数调用中为命名参数设置默认值?

    java - 使用 Windows 锁定屏幕后删除 Kerberos 缓存票证

    docker - 无法连接到主机VM上的SQL Server Express容器

    iis - 我可以使用 Powershell 在远程 IIS 服务器上创建站点和应用程序池吗?

    powershell - Powershell-如何拆分简单的文本文件并覆盖原始文件

    powershell - 使用 PowerShell Invoke-RestMethod 将 Zip 上传到 Phonegap

    python - 如何使用 ldap3 python 模块强制用户在下次登录时更改密码?

    unit-testing - 为内存中的 UnboundID LDAP 服务器创建自定义架构/添加到现有架构

    docker - 如何访问 Docker 容器的 Windows 事件日志

    linux - 如何在 Windows Server 2016 上配置 Service Fabric 群集以便能够运行 Linux 容器?