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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| $InvokeServerName = "SHARING-SERVER"
$SharedFolderPath = "D:\Shares\广州番禺加班有限公司"
$Domain = (Get-ADDomain).Name
$Company_OU = Get-ADOrganizationalUnit -Filter "Name -eq '广州番禺加班有限公司'"
$Company_Group = Get-ADGroup -Filter * -SearchBase $Company_OU -SearchScope OneLevel
$Department_OU_List = Get-ADOrganizationalUnit -Filter * -SearchBase $Company_OU | Where-Object {$_.Name -ne "广州番禺加班有限公司"}
$Boss_User_List = Get-ADUser -Filter "(employeeType -eq '老板') -or ( employeeType -eq '总监')" -SearchBase $Company_OU
$InvokeScriptBlock1 = { New-Item -Path "$Using:SharedFolderPath" -ItemType Directory
New-SmbShare -Name "GZ$" -Path "$Using:SharedFolderPath" -FullAccess "$Using:Domain\$($Using:Company_Group.Name)"
$FolderPath = $Using:SharedFolderPath $FolderAcl = Get-Acl -Path $FolderPath $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\administrator", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow") $FolderAcl.SetAccessRule($AccessRule)
$AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Using:Company_Group.Name)", "ListDirectory", "None", "None", "Allow") $FolderAcl.SetAccessRule($AccessRule)
if( ($Using:Boss_User_List.SamAccountName).Count -gt 0 ){ foreach ($Boss_User in $Using:Boss_User_List){ $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Boss_User.SamAccountName)", "Read", "ContainerInherit,ObjectInherit", "None", "Allow") $FolderAcl.SetAccessRule($AccessRule) } }
$FolderAcl.SetAccessRuleProtection($true,$false) Set-Acl -Path $FolderPath -AclObject $FolderAcl }
Invoke-Command -ComputerName $InvokeServerName -ScriptBlock $InvokeScriptBlock1
foreach ($Department_OU in $Department_OU_List) {
$Department_User_List = Get-ADUser -Filter * -SearchBase $Department_OU
$Director_User_List = Get-ADUser -Filter "employeeType -eq '主管'" -SearchBase $Department_OU
$InvokeScriptBlock2 = {
New-Item -Path "$Using:SharedFolderPath\$($Using:Department_OU.Name)" -ItemType Directory
$FolderPath1 = "$Using:SharedFolderPath\$($Using:Department_OU.Name)" $FolderAcl = Get-Acl -Path $FolderPath1
$AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Using:Company_OU.Name)_$($Using:Department_OU.Name)", "ListDirectory", "None", "None", "Allow") $FolderAcl.SetAccessRule($AccessRule)
if( ($Using:Director_User_List.SamAccountName).Count -gt 0 ){ foreach ($Director_User in $Using:Director_User_List){ $AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Director_User.SamAccountName)", "Read", "ContainerInherit,ObjectInherit", "None", "Allow") $FolderAcl.SetAccessRule($AccessRule) } }
Set-Acl -Path $FolderPath1 -AclObject $FolderAcl
New-Item -Path "$Using:SharedFolderPath\$($Using:Department_OU.Name)\部门共享" -ItemType Directory
$FolderPath2 = "$Using:SharedFolderPath\$($Using:Department_OU.Name)\部门共享" $FolderAcl = Get-Acl -Path $FolderPath2
$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") $FolderAcl.SetAccessRule($AccessRule) Set-Acl -Path $FolderPath2 -AclObject $FolderAcl
foreach ($Department_User in $Using:Department_User_List ) {
New-Item -Path "$Using:SharedFolderPath\$($Using:Department_OU.Name)\$($Department_User.Name)" -ItemType Directory $FolderPath3 = "$Using:SharedFolderPath\$($Using:Department_OU.Name)\$($Department_User.Name)" $FolderAcl = Get-Acl -Path $FolderPath3
$AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule("$Using:Domain\$($Department_User.SamAccountName)", "Modify,ReadAndExecute,Write", "ContainerInherit,ObjectInherit", "None", "Allow") $FolderAcl.SetAccessRule($AccessRule) Set-Acl -Path $FolderPath3 -AclObject $FolderAcl
} }
Invoke-Command -ComputerName $InvokeServerName -ScriptBlock $InvokeScriptBlock2
}
|