Powershell 创建文件,安全组和DFS链接

来源:互联网 发布:数据挖掘 理论与算法 编辑:程序博客网 时间:2024/05/17 21:58

最近工作需要,需要创建80多个文件夹,300多个对应的安全组,把某部门的一堆用户放进对应的组,然后把这些文件夹都设置成DFS的Target的共享对象,然后拷贝文件。


这些工作手工操作的话实在是太繁琐了,豆子考虑用powershell来实现这些功能,毕竟以后也用得着。这里把我的脚本备忘一下,以便日后参考:


基本思路和实现其实都很简单,有个别地方dos命令和powershell的特殊符号无法直接识别,需要一些小技巧处理:


基本过程

  1. 创建用户组

  2. 添加用户到组

  3. 创建新的共享文件夹,添加成新的DFS对象,配置访问权限

  4. Robocopy 拷贝文件内容



上面4个小任务,豆子很屌丝的写了4个对应的小脚本,当然放在一个里面也是可以实现的。


脚本1就是从某个文件里面读取客户名,然后创建对应的组,然后根据我公司的访问要求进行嵌套,具体情况可以根据实际NTFS的需求来更改。里面的Try Catch我也写的很随意,Catch应该根据具体的报错进行输出,这里我都简单化了。


script 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$groups=import-csv c:\temp\newgroups.csv
foreach($groupin $groups){
if($group.name-eq ""){}
else{
$name1="group.fs.clients."+$group.name.trim()
$name2=$name1+".apollo.rw"
$name3=$name1+".apollo.ro"
try{
New-ADGroup -Name $name1 -GroupCategory Security -GroupScope Global -DisplayName $name1 -Path "OU=clients,OU=File System,OU=MCU GROUPS, OU=- MCU.LOCAL -,DC=MITCH,DC=MITCHELLS,DC=COM,DC=AU" -Description "Root Folder Group Container"
write-host $name1 "is created"
}
catch
{
write-host $name1 "is already created"
}
try{
New-ADGroup -Name $name2 -GroupCategory Security -GroupScope Global -DisplayName $name2 -Path "OU=clients,OU=File System,OU=MCU GROUPS, OU=- MCU.LOCAL -,DC=MITCH,DC=MITCHELLS,DC=COM,DC=AU"
write-host $name2 "is created"
}
catch
{
write-host $name2 "is already created"
}
try{
New-ADGroup -Name $name3 -GroupCategory Security -GroupScope Global -DisplayName $name3 -Path "OU=clients,OU=File System,OU=MCU GROUPS, OU=- MCU.LOCAL -,DC=MITCH,DC=MITCHELLS,DC=COM,DC=AU"
write-host $name3 "is created"
}
catch
{
write-host $name3 "is already created"
}
try{
Add-ADGroupmember $name1 $name2,$name3
write-host $name2 "and " $name3 " are added into " $name1
}
catch
{
write-host $name2 "and " $name3 " are already into " $name1
}
try{
Add-ADGroupmember group.dfs.root.business.clients $name1
write-host $name1 "is added into group.dfs.root.business.clients"
}
catch
{
write-host $name1 "is already in group.dfs.root.business.clients"
}
}
write-host ""
}

脚本里面读取的文本格式如下所示

wKiom1P6kbfB2ep9AABh9y91qzI259.png


我们可以指定一个日期,然后查看是否成功创建了这些组,比如我是上周5创建的,然后这个博客是周一写的,所以随手查询一下上周四以后我创建的新组是啥


wKiom1P6k5qwI18cAAAhG25m5W0609.png

类似的,第二个脚本就是一个双重循环,添加成员到组就是了,也很简单。trim的目的是为了去掉Excel文档里面的结束空格字符。


script 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$users=get-contentc:\temp\apollousers.csv
$groups=get-contentc:\temp\apollogroups.csv
foreach($userin $users){
$username=$user.trim()
if($username-eq "") {
}
else
{
write-host $username
foreach($group in $groups){
try{
add-adgroupmember -identity $group.trim() -member $username
write-host $group
}
Catch
{
write-host "Already added into" $group.trim()
}
}
}
write-host ""
}


第三个脚本稍微花了我点时间,因为我里面调用的是DOS命令的icacls来更改权限,不太想用Powershell的set-acl 命令,因为太麻烦,我不想花时间去查.net class。但是 icacls并不能很好的识别powreshell传递的变量,必须用$()的形式才能工作。


另外dfsutil 要求我的服务器必须安装DFS feature, new-smbshare是Powershell 4.0里面的命令,因此只能运行在安装了.net4 的 windows 8 平台之上。


script3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$clients=import-csv c:\temp\dfs1.csv
foreach($clientin $clients){
if($client.name-eq ""){}
else{
$name1="group.fs.clients."+$client.name.trim()
$name2=$name1+".apollo.rw"
$name3=$name1+".apollo.ro"
$dir="M:\DataDisk_1\DFSLinks\clients-"+$client.name.trim()
$dir2="\\mitch.mitchells.com.au\business\clients\"+$client.name.trim()
$dir3="\\auvicfs00.mitch.mitchells.com.au\clients-"+$client.name.trim()+"$"
$nswdfs00="\\aunswdfs00\e$\DFSroot-business\clients\"+$client.name.trim()
$nswdfs01="\\aunswdfs01\e$\DFSroot-business\clients\"+$client.name.trim()
$vicdfs00="\\auvicdc00\e$\DFSroot-business\clients\"+$client.name.trim()
$vicdfs01="\\auvicdc01\e$\DFSroot-business\clients\"+$client.name.trim()
$perm1=':(R)'
$perm2=':(OI)(CI)(F)'
$perm3=':(OI)(CI)(R)'
$sharename="clients-"+$client.name.trim()+"$"
write-host $sharename
mkdir $dir\apollo
New-SmbShare -name "$($sharename)" -Path $dir -FullAccess Everyone -cachingmode none
icacls $dir /grant "$($name1)$perm1"
icacls $dir\apollo /grant "$($name2)$perm2"
icacls $dir\apollo /grant "$($name3)$perm3"
dfsutil link add $dir2 $dir3
icacls $nswdfs00 /grant "$($name1)$perm1"
icacls $nswdfs01 /grant "$($name1)$perm1"
icacls $vicdfs00 /grant "$($name1)$perm1"
icacls $vicdfs01 /grant "$($name1)$perm1"
}
}


创建完了之后,可以通过Share和DFS进行确认。


第四个脚本就是简单的robocopy脚本,我这里不需要保留源文件的权限,所以简单的


robocopy source destination /E 就可以了

0 0
原创粉丝点击