Solved

Adding additional text to each item in a Powershell array

Posted on 2013-12-09
11
1,630 Views
Last Modified: 2013-12-19
How do I add a constant text string to the end of the text of each item in an array?

I am using Powershell to list the folders in a specific location but need to add a subfolder "\Emails" to each item before I can use it to set the Email folder permissions

$FolderList = dir | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName }
0
Comment
Question by:ARampton
  • 6
  • 4
11 Comments
 
LVL 30

Expert Comment

by:renazonse
ID: 39706470
foreach ($Folder in $FolderList) {
    $FullPath = "$Folder" + "\Emails"
    echo $FullPath
}

Open in new window

0
 
LVL 39

Expert Comment

by:footech
ID: 39706504
You can use concatentation (the plus sign), subexpressions, or the format specifier (-f) and variable substitution.  I prefer subexpressions in many cases.  If you wanted to modify the contents of your $FolderList array, you can use the below.
$FolderList = dir | Where-Object { $_.PSIsContainer } |
 ForEach-Object { "$($_.FullName)\Emails" } 

Open in new window

0
 

Author Comment

by:ARampton
ID: 39707073
Great renazonse - works just as I wanted - I am just learning some of Powershell

What is the simplest way of now using $FullPath with set-ACL for each folder which now correctly lists in $FullPath?

I used  $NewACL = Get-Acl "S:\SampleFolder\Emails"  where this Emails folder has the permissions I want copied and  Set-Acl "S:\Project XXXX\Emails" $NewACL  but one folder at a time

I would like it to apply Set-Acl to all folders listed in $FullPath
0
 
LVL 30

Expert Comment

by:renazonse
ID: 39707264
$FolderList = dir | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName }
$NewACL = Get-Acl "S:\SampleFolder\Emails"

foreach ($Folder in $FolderList) {
    $FullPath = "$Folder" + "\Emails"
     Set-Acl "$FullPath" $NewACL
}

Open in new window

0
 

Author Comment

by:ARampton
ID: 39713772
Thanks renazonse

Your script achieves just what I need and I understand the sequence except for why the $Folder is in quotes in the line $FullPath = "$Folder" + "\Emails"

How would I amend this script to make the process also include any subfolders below \Emails so making t recursive?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 30

Accepted Solution

by:
renazonse earned 500 total points
ID: 39713800
$FolderList = dir | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName }
$NewACL = Get-Acl "S:\SampleFolder\Emails"
foreach ($Folder in $FolderList) {
    $FullPath = "$Folder" + "\Emails"
    Get-ChildItem $FullPath -recurse -Force |%{
   $NewACL | Set-Acl -Path $_.fullname
}
}

The quotes are they in case there's spaces in the path. Although you may not need it there.
0
 

Author Comment

by:ARampton
ID: 39713850
Thanks, this is really useful in applying the same ACLs to all target subfolders

How would this script change when the permissions below the source \Emails vary on each source subfolder and need to set the same on the target subfolders which always mirror the source structure

I think recursive code needs to be adding to the Get-Acl but have never used this before
0
 
LVL 30

Expert Comment

by:renazonse
ID: 39713950
That's a bit more in depth and would require another question
0
 

Author Comment

by:ARampton
ID: 39714710
New Question Raised

Powershell script to copy ACL Permission from source folder structure to target folder structure
0
 

Author Comment

by:ARampton
ID: 39728920
From your help I have developed this script to Copy ACL Permissions from Source to Target item by item (included folders and files)

$SFolder = "S:\SourceFolder"
$TFolder = "S:\3060\3078 Test Project 8"     'Targert Folder

$SFolderList = get-childitem -name $SFolder -recurse
foreach ($Folder in $SFolderList) {
          $SFullPath = $SFolder + "\" + "$Folder"
          $TFullPath = $TFolder + "\" + "$Folder"
      $NewACL = Get-ACL "$SFullPath"
Set-ACL "$TFullPath" $NewACL
echo "ACL copied to $TFolder $Folder"
}

Works just how I wanted and is useful in resetting or updating project folder permissions after they have been setup
0
 

Author Closing Comment

by:ARampton
ID: 39728922
Really good script which I have used frequently since
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article will help you understand what HashTables are and how to use them in PowerShell.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now