当前位置: 动力学知识库 > 问答 > 编程问答 >

powershell - Import-CSV and ForEach providing unexpected output

问题描述:

I'm new here, and this is my first question. I looked through various related questions, but was not able to find an answer, so I'm hopeful that someone can help.

I have a script to create folders for user accounts, and assign them privileges. I have a .csv file with the names of the folders I want to create. When I run the script, it is not producing the desired results, but I'm not sure where I'm going wrong.

Contents of .csv file (each on a separate line):

1001

1002

1003...

There are no headers, just the numbers.

PS Script I'm running:

$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}

This calls the folder creation script, which looks like this (I found this, it's not mine):

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

}

The output I'm getting is:

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}

It creates a folder with the name '@{Name=102}', when all I want is '102'.

I've also tried this without the header, by adding a line at the top of my .csv file called 'Name', but had no success there either. If I don't include a header, it tries to use the first folder name as the header.

If I run this script manually (without feeding it the .csv), it runs successfully, but I have over a thousand folders to create, so I'd rather automate. I'm sure this is something simple, as I'm new to PS scripting.

Thanks in advance for any help!

网友答案:

So, you have a CSV with no header and one column that you are importing. Currently the way you are importing it is populating $csv with and object array with the property name. The rest of your script is treating that as just a string array and ignoring the property. A minor change that would address this problem is to expand that column.

$csv = Import-Csv "E:\Support\usersOnly.csv" -Header @("Name") | Select-Object -ExpandProperty Name

But really since it is only one column and no header don't even bother with the CSV aspect of this (it really isn't a CSV anyway.)

$csv = Get-Content "E:\Support\usersOnly.csv"

Aside from the potential misleading variable name you should just have to change that one line for this to be correct. (Assuming there are not other problems.)

The @{Name=1018} you are seeing is the string representation (shown as a hashtable) of one of the elements in your $csv array.

分享给朋友:
您可能感兴趣的文章:
随机阅读: