Powershell多维数组迭代
Powershell Multi-Dimensional Array Iteration
Powershell 相当新。我正在尝试将一些不同的概念应用于此解决方案。
我想创建一个文件夹,然后使用一个旨在帮助将 ACL 设置为文件夹的功能。一个多维数组,旨在保存文件夹名称模板,以用于各种目的和部门。数字只是占位符。
我 运行 添加了假设,我没有收到任何错误,但感觉我还是在某个地方犯了错误。
我在 Powershell 中研究了很多循环和遍历多维数组的方法,但没有找到与我的意图非常匹配的方法。
function Set-permissions {
Param($assign_to_Read, $assign_to_RW, $path)
#Create new ACL Object
$acl_main = New-Object System.Security.AccessControl.DirectorySecurity
#Create Read Object
$ACL_Read = New-Object System.Security.AccessControl.FileSystemAccessRule($assign_to_Read, "Read", "Allow")
#Set first access object to ACL: Read
$acl_main.SetAccessRule($ACL_Read)
#Create Modify Object
$ACL_RW = New-Object System.Security.AccessControl.FileSystemAccessRule($assign_to_RW, "Modify", "Allow")
#Add access object to ACL: Modify
$acl_main.AddAccessRule($ACL_RW)
#Pipe the
$acl_main | Set-Acl -Path $path -WhatIf
}
New-Item -Path $top_level -Name $result -ItemType "directory"
#Create Subdirectories and Assign Read and RW ACL
$newEmployeePath = $top_level + '\' + $result
$employee_template = @(
("1 ",
"ACL_Read_1 ",
"ACL_RW_1 "),
("2 ",
"ACL_Read_2 ",
"ACL_RW_2 "),
("3 ",
"ACL_Read_3 ",
"ACL_RW_3 "),
)
ForEach($folder in $employee_template ){
#Create the Folder from first location in the array
New-Item -Path $newEmployeePath -Name $folder[0]
#Set permissions for Read and RW on the folder.
Set-permissions $folder[1] $folder[2] $newEmployeePath
}
}
我认为你最好使用 objects 的数组来代替:
$employee_template = [PsCustomObject]@{Folder = 'EmployeeFolder1'; ReadAccess = 'EmployeeOrGroupRead1'; WriteAccess = 'EmployeeOrGroupWrite1'},
[PsCustomObject]@{Folder = 'EmployeeFolder2'; ReadAccess = 'EmployeeOrGroupRead2'; WriteAccess = 'EmployeeOrGroupWrite2'},
[PsCustomObject]@{Folder = 'EmployeeFolder3'; ReadAccess = 'EmployeeOrGroupRead3'; WriteAccess = 'EmployeeOrGroupWrite3'}
# and so on
然后像这样使用该对象数组:
foreach ($employee in $employee_template) {
# Create the Folder (suppress console output)
$newEmployeePath = Join-Path -Path $top_level -ChildPath $employee.Folder
$null = New-Item -Path $newEmployeePath -ItemType Directory -Force
# Set permissions for Read and RW on the folder.
Set-Permissions $newEmployeePath $employee.ReadAccess $employee.WriteAccess
}
P.S。对于上述情况,要么将函数中的参数顺序更改为更合乎逻辑的
Param($path, $assign_to_Read, $assign_to_RW)
或使用
调用函数
Set-Permissions $employee.ReadAccess $employee.WriteAccess $newEmployeePath
当然,您可以轻松创建 CSV 文件并使用 Import-Csv
获取员工文件夹和访问的对象数组,这样您就不必在代码中创建它:
CSV 示例:
"Folder","ReadAccess","WriteAccess"
"EmployeeFolder1","EmployeeOrGroupRead1","EmployeeOrGroupWrite1"
"EmployeeFolder2","EmployeeOrGroupRead2","EmployeeOrGroupWrite2"
"EmployeeFolder3","EmployeeOrGroupRead3","EmployeeOrGroupWrite3"
Powershell 相当新。我正在尝试将一些不同的概念应用于此解决方案。
我想创建一个文件夹,然后使用一个旨在帮助将 ACL 设置为文件夹的功能。一个多维数组,旨在保存文件夹名称模板,以用于各种目的和部门。数字只是占位符。
我 运行 添加了假设,我没有收到任何错误,但感觉我还是在某个地方犯了错误。
我在 Powershell 中研究了很多循环和遍历多维数组的方法,但没有找到与我的意图非常匹配的方法。
function Set-permissions {
Param($assign_to_Read, $assign_to_RW, $path)
#Create new ACL Object
$acl_main = New-Object System.Security.AccessControl.DirectorySecurity
#Create Read Object
$ACL_Read = New-Object System.Security.AccessControl.FileSystemAccessRule($assign_to_Read, "Read", "Allow")
#Set first access object to ACL: Read
$acl_main.SetAccessRule($ACL_Read)
#Create Modify Object
$ACL_RW = New-Object System.Security.AccessControl.FileSystemAccessRule($assign_to_RW, "Modify", "Allow")
#Add access object to ACL: Modify
$acl_main.AddAccessRule($ACL_RW)
#Pipe the
$acl_main | Set-Acl -Path $path -WhatIf
}
New-Item -Path $top_level -Name $result -ItemType "directory"
#Create Subdirectories and Assign Read and RW ACL
$newEmployeePath = $top_level + '\' + $result
$employee_template = @(
("1 ",
"ACL_Read_1 ",
"ACL_RW_1 "),
("2 ",
"ACL_Read_2 ",
"ACL_RW_2 "),
("3 ",
"ACL_Read_3 ",
"ACL_RW_3 "),
)
ForEach($folder in $employee_template ){
#Create the Folder from first location in the array
New-Item -Path $newEmployeePath -Name $folder[0]
#Set permissions for Read and RW on the folder.
Set-permissions $folder[1] $folder[2] $newEmployeePath
}
}
我认为你最好使用 objects 的数组来代替:
$employee_template = [PsCustomObject]@{Folder = 'EmployeeFolder1'; ReadAccess = 'EmployeeOrGroupRead1'; WriteAccess = 'EmployeeOrGroupWrite1'},
[PsCustomObject]@{Folder = 'EmployeeFolder2'; ReadAccess = 'EmployeeOrGroupRead2'; WriteAccess = 'EmployeeOrGroupWrite2'},
[PsCustomObject]@{Folder = 'EmployeeFolder3'; ReadAccess = 'EmployeeOrGroupRead3'; WriteAccess = 'EmployeeOrGroupWrite3'}
# and so on
然后像这样使用该对象数组:
foreach ($employee in $employee_template) {
# Create the Folder (suppress console output)
$newEmployeePath = Join-Path -Path $top_level -ChildPath $employee.Folder
$null = New-Item -Path $newEmployeePath -ItemType Directory -Force
# Set permissions for Read and RW on the folder.
Set-Permissions $newEmployeePath $employee.ReadAccess $employee.WriteAccess
}
P.S。对于上述情况,要么将函数中的参数顺序更改为更合乎逻辑的
Param($path, $assign_to_Read, $assign_to_RW)
或使用
调用函数Set-Permissions $employee.ReadAccess $employee.WriteAccess $newEmployeePath
当然,您可以轻松创建 CSV 文件并使用 Import-Csv
获取员工文件夹和访问的对象数组,这样您就不必在代码中创建它:
CSV 示例:
"Folder","ReadAccess","WriteAccess"
"EmployeeFolder1","EmployeeOrGroupRead1","EmployeeOrGroupWrite1"
"EmployeeFolder2","EmployeeOrGroupRead2","EmployeeOrGroupWrite2"
"EmployeeFolder3","EmployeeOrGroupRead3","EmployeeOrGroupWrite3"