准备

假设人事部给了你一份Excel表,记录了工号、姓名、部门、职位、公司,要按这表在AD域控上面创建对应的用户。

你稍加处理过后另存成了csv,内容格式如下:

工号姓名部门员工类型公司
1001赵一管理层老板广州番禺加班有限公司
1002钱二管理层总监广州番禺加班有限公司
1003孙三管理层经理广州番禺加班有限公司
1004李四管理层经理广州番禺加班有限公司
1005周五管理层经理广州番禺加班有限公司
1006吴六行政部主管广州番禺加班有限公司
1007郑七行政部员工广州番禺加班有限公司
1008王七加加行政部员工广州番禺加班有限公司
1009冯九行政部员工广州番禺加班有限公司
1010陈十行政部员工广州番禺加班有限公司
1011褚十一账务部主管广州番禺加班有限公司
1012卫十二账务部员工广州番禺加班有限公司
1013蒋十三账务部员工广州番禺加班有限公司
1014沈十四账务部员工广州番禺加班有限公司
1015韩十五账务部员工广州番禺加班有限公司
1016杨十六销售部主管广州番禺加班有限公司
1017朱十七销售部员工广州番禺加班有限公司
1018秦十八销售部员工广州番禺加班有限公司
1019尤十九销售部员工广州番禺加班有限公司
1020许二十销售部员工广州番禺加班有限公司
1021何二一后勤部主管广州番禺加班有限公司
1022吕二二后勤部员工广州番禺加班有限公司
1023施二三后勤部员工广州番禺加班有限公司
1024张二四后勤部员工广州番禺加班有限公司
1025孔二五后勤部员工广州番禺加班有限公司
1026曹二六技术部主管广州番禺加班有限公司
1027严二七技术部员工广州番禺加班有限公司
1028华二八技术部员工广州番禺加班有限公司
1029金二九技术部员工广州番禺加班有限公司
1030魏三十技术部员工广州番禺加班有限公司

需求

域账号需求:

域控的层级结构为:公司>部门>员工

以工号来当登陆名,初始密码和工号一样,首次后登陆后必须更改密码。

共享文件夹需求:

文件结构为: \\服务器\公司$\部门\员工

权限要求为:

老板、总监可以查看所有部门

部门之间不可互相查看

员工之间不可互相查看

员工对自己的文件夹可以读写

部门内有一个“部门共享”文件夹,部门内所有员工都可以读写

主管可以查看自己的整个部门所有员工的文件夹

所需命令

简单说明一下要用的命令

操作AD域要用的 Get-ADxxxSet-ADxxx 这一类的命令要添加 Windows PowerShell 的 Active Directory 模块 才能使用。

添加了 Active Directory 域服务 的服务器,会自动加上去的,没有的话也能手动添加上去,没加域和没用对应的域权限运行是用不了的。

脚本里面的具体每条命令我就不解释了,自己去官网查一下吧,比我说得详细,用法实例结合参考我这里贴出来的命令。

服务器在 添加角色和功能向导 里面

Windows Server 2019 添加角色和功能向导 界面

Windows10就是在设置里面可选功能里面找

Windows10 设置 添加可靠功能 界面

批量创建OU、组、用户

POWERSHELL
 1# 根目录路径,批量在此路径下添加OU和用户,我这里直接用了域的根目录
 2$OuRoot = Get-ADDomain | Select-Object -ExpandProperty "DistinguishedName"
 3
 4# 域名
 5$DomainName = (Get-ADDomain).DNSRoot
 6
 7# 导入整理好的csv
 8$UserList = Import-Csv "C:\Users\Administrator\Desktop\工号部门信息表.csv" -Encoding UTF8
 9
10foreach ( $User in $UserList ) {
11
12    # 没有公司就创建公司
13    # 获取根目录OU下面的公司OU的DistinguishedName,拿不到就是没有
14    $OuRoot_Company = Get-ADOrganizationalUnit -Filter "Name -eq '$($User.公司)'" -SearchBase $OuRoot -SearchScope OneLevel | Select-Object -ExpandProperty "DistinguishedName"
15
16    if ( -Not $OuRoot_Company ) {
17        # 创建公司OU
18        New-ADOrganizationalUnit -Name $User.公司 -Path $OuRoot # -ProtectedFromAccidentalDeletion $False
19        # 获取公司OU 的DistinguishedName
20        $OuRoot_Company = Get-ADOrganizationalUnit -Filter "Name -eq '$($User.公司)'" -SearchBase $OuRoot -SearchScope OneLevel | Select-Object -ExpandProperty "DistinguishedName"
21        # 创建公司OU 的组
22        New-ADGroup -Name $User.公司 -Path $OuRoot_Company -GroupScope Global
23    }
24
25
26    # 公司下面没有部门就创建部门
27    # 获取公司下面的部门OU的DistinguishedName,拿不到就是没有
28    $OuRoot_Company_Department = Get-ADOrganizationalUnit -Filter "Name -eq '$($User.部门)'" -SearchBase $OuRoot_Company -SearchScope OneLevel | Select-Object -ExpandProperty "DistinguishedName"
29
30    if ( -Not $OuRoot_Company_Department ) {
31        # 创建公司下的部门OU
32        New-ADOrganizationalUnit -Name $User.部门 -Path $OuRoot_Company # -ProtectedFromAccidentalDeletion $False
33        # 获取公司下的部门OU 的DistinguishedName
34        $OuRoot_Company_Department = Get-ADOrganizationalUnit -Filter "Name -eq '$($User.部门)'" -SearchBase $OuRoot_Company -SearchScope OneLevel | Select-Object -ExpandProperty "DistinguishedName"
35        # 创建公司下的部门OU 的组,为什么这里要用“公司_部门”?因为可能有两个子公司,两个子公司都有财务部,只有一个公司可以去掉公司前缀
36        New-ADGroup -Name $($($User.公司) + "_" + $($User.部门) ) -Path $OuRoot_Company_Department -GroupScope Global
37    }
38
39
40    # 创建用户,相关属性自行增减,这里为了登陆的时候直接使用工号当账号密码,域服务器的本地组策略的密码复杂度是关掉的了。
41    New-ADUser `
42        -Name $User.姓名 `
43        -DisplayName $User.姓名 `
44        -EmployeeNumber $User.工号 `
45        -SamAccountName $User.工号 `
46        -UserPrincipalName "$($User.工号)@$DomainName" `
47        -Department $User.部门 `
48        -OtherAttributes @{EmployeeType=$($User.员工类型)} `
49        -AccountPassword $(ConvertTo-SecureString $User.工号 -AsPlainText -Force) `
50        -ChangePasswordAtLogon $True `
51        -Enabled $True `
52        -Path $OuRoot_Company_Department
53
54    # 获取用户的 DistinguishedName
55    $User_DN = Get-ADUser -identity $User.工号 | Select-Object -ExpandProperty "DistinguishedName"
56
57    # 把用户添加进公司_部门的组
58    $Company_Department_Group_DN = Get-ADGroup -identity $($($User.公司) + "_" + $($User.部门) ) | Select-Object -ExpandProperty "DistinguishedName"
59    Add-ADGroupMember -Identity $Company_Department_Group_DN -Members $User_DN
60
61    # 把部门添加进公司的组
62    $Company_Group_DN = Get-ADGroup -identity $User.公司 | Select-Object -ExpandProperty "DistinguishedName"
63    Add-ADGroupMember -Identity $Company_Group_DN -Members $Company_Department_Group_DN
64
65}
点击展开查看更多

批量创建共享文件夹

因为要拿AD域里面的信息,又懒得在文件共享服务器那边装模块(模拟可能的实际场景),所以这里用的是远程PowerShell命令

关于PowerShell设置文件权限的信息,可以看我的另一篇文章:

PowerShell 修改文件夹及文件权限 - tjxwork (tjxblog.com)

POWERSHELL
  1# 要远程的共享文件服务器的名字(已加域)
  2$InvokeServerName = "SHARING-SERVER"
  3
  4# 要在共享服务器上面的创建的共享文件夹的路径
  5$SharedFolderPath = "D:\Shares\广州番禺加班有限公司" 
  6
  7# 获取域
  8$Domain = (Get-ADDomain).Name
  9
 10# 获取公司 OU
 11$Company_OU = Get-ADOrganizationalUnit -Filter "Name -eq '广州番禺加班有限公司'"
 12
 13# 获取公司 组
 14$Company_Group = Get-ADGroup -Filter * -SearchBase $Company_OU -SearchScope OneLevel
 15
 16# 获取公司下面的所有部门
 17$Department_OU_List = Get-ADOrganizationalUnit -Filter * -SearchBase $Company_OU  | Where-Object {$_.Name -ne "广州番禺加班有限公司"}
 18
 19# 获取公司下所有员工属性为老板和总监的员工
 20$Boss_User_List = Get-ADUser -Filter "(employeeType -eq '老板') -or ( employeeType -eq '总监')" -SearchBase $Company_OU
 21
 22
 23# 创建最顶层共享文件夹
 24$InvokeScriptBlock1 = {
 25        # 新建公司共享文件夹路径
 26        New-Item -Path "$Using:SharedFolderPath" -ItemType Directory
 27
 28        # 设置公司共享文件夹,隐藏共享,共享路径是 \\服务器\GZ$
 29        New-SmbShare -Name "GZ$" -Path "$Using:SharedFolderPath" -FullAccess "$Using:Domain\$($Using:Company_Group.Name)"
 30
 31        # 准备修改文件夹权限
 32        $FolderPath = $Using:SharedFolderPath
 33        $FolderAcl = Get-Acl -Path $FolderPath
 34        
 35        # 设置 域 Administrator 的权限
 36        $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\administrator", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
 37        $FolderAcl.SetAccessRule($AccessRule)
 38
 39        # 设置 公司 组的权限
 40        $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Using:Company_Group.Name)", "ListDirectory", "None", "None", "Allow")
 41        $FolderAcl.SetAccessRule($AccessRule)
 42
 43        # 设置 老板、总监的权限
 44        if( ($Using:Boss_User_List.SamAccountName).Count -gt 0 ){
 45            foreach ($Boss_User in $Using:Boss_User_List){
 46                $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Boss_User.SamAccountName)", "Read", "ContainerInherit,ObjectInherit", "None", "Allow")
 47                $FolderAcl.SetAccessRule($AccessRule)
 48            }
 49        }
 50
 51        # 禁用继承权限
 52        $FolderAcl.SetAccessRuleProtection($true,$false)
 53		
 54		# 应用到文件夹
 55        Set-Acl -Path $FolderPath -AclObject $FolderAcl
 56}
 57
 58Invoke-Command -ComputerName $InvokeServerName -ScriptBlock $InvokeScriptBlock1
 59
 60
 61
 62# 以部门进行循环
 63foreach ($Department_OU in $Department_OU_List) {
 64
 65    # 获取部门下的所有用户
 66    $Department_User_List = Get-ADUser -Filter * -SearchBase $Department_OU
 67
 68    # 获取部门下所有员工属性为主管的员工
 69    $Director_User_List = Get-ADUser -Filter "employeeType -eq '主管'" -SearchBase $Department_OU
 70
 71    # 远程执行的代码块2
 72    $InvokeScriptBlock2 = {
 73
 74        # 新建部门文件夹
 75        New-Item -Path "$Using:SharedFolderPath\$($Using:Department_OU.Name)" -ItemType Directory
 76
 77        # 准备修改文件夹权限
 78        $FolderPath1 = "$Using:SharedFolderPath\$($Using:Department_OU.Name)"
 79        $FolderAcl = Get-Acl -Path $FolderPath1
 80
 81        # 设置 部门 组的权限
 82        $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Using:Company_OU.Name)_$($Using:Department_OU.Name)", "ListDirectory", "None", "None", "Allow")
 83        $FolderAcl.SetAccessRule($AccessRule)
 84
 85        if( ($Using:Director_User_List.SamAccountName).Count -gt 0 ){
 86            # 设置 主管的权限
 87            foreach ($Director_User in $Using:Director_User_List){
 88                $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Director_User.SamAccountName)", "Read", "ContainerInherit,ObjectInherit", "None", "Allow")
 89                $FolderAcl.SetAccessRule($AccessRule)
 90            }
 91        }
 92
 93        # 应用到文件夹
 94        Set-Acl -Path $FolderPath1 -AclObject $FolderAcl
 95
 96        # 新建部门内“部门共享”文件夹
 97        New-Item -Path "$Using:SharedFolderPath\$($Using:Department_OU.Name)\部门共享" -ItemType Directory
 98
 99        # 准备修改文件夹权限
100        $FolderPath2 = "$Using:SharedFolderPath\$($Using:Department_OU.Name)\部门共享"
101        $FolderAcl = Get-Acl -Path $FolderPath2
102
103        # 设置 部门\部门共享 组的权限
104        $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Using:Company_OU.Name)_$($Using:Department_OU.Name)", "Modify,ReadAndExecute,Write", "ContainerInherit,ObjectInherit", "None", "Allow")
105        $FolderAcl.SetAccessRule($AccessRule)
106        
107        # 应用到文件夹
108        Set-Acl -Path $FolderPath2 -AclObject $FolderAcl
109
110		# 循环创建用户文件夹
111        foreach ($Department_User in $Using:Department_User_List ) {
112
113            # 新建用户文件夹
114            New-Item -Path "$Using:SharedFolderPath\$($Using:Department_OU.Name)\$($Department_User.Name)" -ItemType Directory
115    
116            # 准备修改文件夹权限
117            $FolderPath3 = "$Using:SharedFolderPath\$($Using:Department_OU.Name)\$($Department_User.Name)"
118            $FolderAcl = Get-Acl -Path $FolderPath3
119
120            # 设置用户权限
121            $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Department_User.SamAccountName)", "Modify,ReadAndExecute,Write", "ContainerInherit,ObjectInherit", "None", "Allow")
122            $FolderAcl.SetAccessRule($AccessRule)
123            
124            # 应用到文件夹
125            Set-Acl -Path $FolderPath3 -AclObject $FolderAcl
126
127        }
128    }
129
130    Invoke-Command -ComputerName $InvokeServerName -ScriptBlock $InvokeScriptBlock2
131
132}
点击展开查看更多

个人不太建议写长脚本的,测试修改起来挺麻烦的,宁愿拆分开多操作几次。


原文作者:tjxwork
原文链接:https://www.tjxblog.com/blog/2023-0006
发布时间:2023-02-16

版权声明

作者: tjxwork

链接: https://tjxblog.com/blog/2023-0006/

许可证: CC BY-NC-SA 4.0

使用者可以对本创作进行转载、节选、混编、二次创作,但不得运用于商业目的,且使用时须进行署名,采用本创作的内容必须同样采用本协议进行授权。

评论

开始搜索

输入关键词搜索文章内容

↑↓
ESC
⌘K 快捷键