0

I hope someone can help me. I am pretty new to PowerShell and can't really script it myself with the exception of looking at existing code and modifying it.

I have found a PowerShell script that reports file share permissions for a specific share and recurses through the subfolders returning their permissions as well.

My problem is I need to do this with a lot of shares so would like to be able to provide the script with a text file containing the share names. I know I need to do a for each loop and read the names of the shares in a text file into an array but I don't know how to do this. I guess it's pretty simple for someone with more experience.

This is the script i have used with single entry.

http://mywinsysadm.wordpress.com/2011/08/17/powershell-reporting-ntfs-permissions-of-windows-file-shares/

#Set variables
$path = Read-Host "Enter the path you wish to check"
$filename = Read-Host "Enter Output File Name"
$date = Get-Date

#Place Headers on out-put file
$list = "Permissions for directories in: $Path"
$list | format-table | Out-File "C:\scripts\$filename"
$datelist = "Report Run Time: $date"
$datelist | format-table | Out-File -append "C:\scripts\$filename"
$spacelist = " "
$spacelist | format-table | Out-File -append "C:\scripts\$filename"

#Populate Folders & Files Array
[Array] $files = Get-ChildItem -path $path -force -recurse

#Process data in array
ForEach ($file in [Array] $files)
{
#Convert Powershell Provider Folder Path to standard folder path
$PSPath = (Convert-Path $file.pspath)
$list = ("Path: $PSPath")
$list | format-table | Out-File -append "C:\scripts\$filename"

Get-Acl -path $PSPath | Format-List -property AccessToString | Out-File -append "C:\scripts\$filename"

} #end ForEach

Sorry for the noob question. I plan to learn more when I have a bit more time but any help now would be massively appreciated.

Thanks in advance.

1
  • Can you do a Google search for "powershell read file" and "powershell loops"? Those two topics will help you along. Commented Aug 15, 2013 at 12:17

1 Answer 1

9

If you have a share name on each line within your text file can put all the shares into an array like this:

$path = "C:\ShareNames.txt"
$shareArray = gc $path

To access the first share you can use this syntax:

$shareArray[0] 
Sign up to request clarification or add additional context in comments.

2 Comments

This is a very inefficient approach. You don't need to declare an empty array and sequentially add to it (which requires allocating a new array at every step, plus extra code). Just do $shareArray = gc $path. Done!
If there is only one line, the above solution won't create an array, but this will: $shareArray = (Get-Content "C:\ShareNames.txt").Split([Environment]::NewLine)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.