Solved

Adding additional text to each item in a Powershell array

Posted on 2013-12-09
11
1,724 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
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…

919 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

18 Experts available now in Live!

Get 1:1 Help Now