【发布时间】:2015-08-24 15:05:39
【问题描述】:
我是新来的,这是我的第一个问题。我浏览了各种相关问题,但无法找到答案,所以我希望有人能提供帮助。
我有一个脚本可以为用户帐户创建文件夹并为其分配权限。我有一个 .csv 文件,其中包含我要创建的文件夹的名称。当我运行脚本时,它没有产生预期的结果,但我不确定我哪里出错了。
.csv 文件的内容(各占一行):
1001
1002
1003...
没有标题,只有数字。
我正在运行的 PS 脚本:
$csv = Import-Csv "E:\Support\usersOnly.csv" -Header @("Name")
ForEach ($line in $csv) {E:\Support\CreateFolders.ps1 -Path E:\blah\blahblah\$line -Access mydomain\$line -Permission modify}
这会调用文件夹创建脚本,长这样(我找到了,不是我的):
param ([string]$Path, [string]$Access, [string]$Permission = ("Modify"), [switch]$help)
function GetHelp() {
$HelpText = @"
DESCRIPTION:
NAME: CreateFolder.ps1
Create Folders and sets permissions for user.
PARAMETERS:
-Path Folder to Create or Modify (Required)
-User User who should have access (Required)
-Permission Specify Permission for User, Default set to Modify (Optional)
-help Prints the HelpFile (Optional)
SYNTAX:
./CreateFolder.ps1 -Path C:\Folder\NewFolder -Access Domain\UserName -Permission FullControl
Creates the folder C:\Folder\NewFolder if it doesn't exist.
Sets Full Control for Domain\UserName
./CreateFolder.ps1 -Path C:\Folder\NewFolder -Access Domain\UserName
Creates the folder C:\Folder\NewFolder if it doesn't exist.
Sets Modify (Default Value) for Domain\UserName
./CreateFolder.ps1 -help
Displays the help topic for the script
Below Are Available Values for -Permission
"@
$HelpText
[system.enum]::getnames([System.Security.AccessControl.FileSystemRights])
}
function CreateFolder ([string]$Path) {
# Check if the folder Exists
if (Test-Path $Path) {
Write-Host "Folder: $Path Already Exists" -ForeGroundColor Yellow
} else {
Write-Host "Creating $Path" -Foregroundcolor Green
New-Item -Path $Path -type directory | Out-Null
}
}
function SetAcl ([string]$Path, [string]$Access, [string]$Permission) {
# Get ACL on FOlder
$GetACL = Get-Acl $Path
# Set up AccessRule
$Allinherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit"
$Allpropagation = [system.security.accesscontrol.PropagationFlags]"None"
$AccessRule = New-Object system.security.AccessControl.FileSystemAccessRule($Access, $Permission, $AllInherit, $Allpropagation, "Allow")
# Check if Access Already Exists
if ($GetACL.Access | Where { $_.IdentityReference -eq $Access}) {
Write-Host "Modifying Permissions For: $Access" -ForeGroundColor Yellow
$AccessModification = New-Object system.security.AccessControl.AccessControlModification
$AccessModification.value__ = 2
$Modification = $False
$GetACL.ModifyAccessRule($AccessModification, $AccessRule, [ref]$Modification) | Out-Null
} else {
Write-Host "Adding Permission: $Permission For: $Access"
$GetACL.AddAccessRule($AccessRule)
}
Set-Acl -aclobject $GetACL -Path $Path
Write-Host "Permission: $Permission Set For: $Access" -ForeGroundColor Green
}
if ($help) { GetHelp }
if ($Path -AND $Access -AND $Permission) {
CreateFolder $Path
SetAcl $Path $Access $Permission
}
我得到的输出是:
Permission: modify Set For: mydomain\@{Name=1018}
Creating E:\blah\blahblah\@{Name=1019}
Adding Permission: modify For: mydomain\@{Name=1019}
Exception calling "AddAccessRule" with "1" argument(s): "Some or all identity references could not be translated."
At E:\Support\CreateFolders.ps1:90 char:3
+ $GetACL.AddAccessRule($AccessRule)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IdentityNotMappedException
Permission: modify Set For: mydomain\@{Name=1019}
Creating E:\blah\blahblah\@{Name=102}
Adding Permission: modify For: mydomain\@{Name=102}
它会创建一个名为“@{Name=102}”的文件夹,而我想要的只是“102”。
我也尝试过不带标题的方法,方法是在我的 .csv 文件顶部添加一个名为“名称”的行,但在那里也没有成功。如果我不包含标题,它会尝试使用第一个文件夹名称作为标题。
如果我手动运行此脚本(不提供 .csv),它会成功运行,但我有超过一千个文件夹要创建,所以我宁愿自动化。我确信这很简单,因为我是 PS 脚本的新手。
提前感谢您的帮助!
【问题讨论】:
-
我看到的第一件事是在
$csv = Import-Csv "E:\Support\usersOnly.csv" -Header @("Name")附近。您有一个带有name属性的对象数组。考虑在开始时尝试这个。$csv = Import-Csv "E:\Support\usersOnly.csv" -Header @("Name") | Select-Object -Expand Name。在阅读了您的更多帖子后,我认为这是问题所在。答案即将到来。
标签: powershell csv